{"id":14535,"date":"2016-05-13T10:33:26","date_gmt":"2016-05-13T14:33:26","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=14535"},"modified":"2016-05-13T10:35:47","modified_gmt":"2016-05-13T14:35:47","slug":"packing-bytes-in-swift","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2016\/05\/13\/packing-bytes-in-swift\/","title":{"rendered":"Packing Bytes in Swift"},"content":{"rendered":"<p><a href=\"http:\/\/www.russbishop.net\/packing-bytes-in-swift\">Russ Bishop<\/a>:<\/p>\n<blockquote cite=\"http:\/\/www.russbishop.net\/packing-bytes-in-swift\">\n<p>I want to do this purely in Swift and mostly analogous to what you&rsquo;d do in C because it&rsquo;s relatively quick, doesn&rsquo;t introduce any dependencies, and the decoder is really simple and could be implemented on any platform.<\/p>\n<p>[&#8230;]<\/p>\n<p>We can calculate the appropriate location in the buffer by performing arithmetic on the <code>UnsafeMutablePointer<\/code> itself. The key is that unsafe pointers in Swift know the size of the type they are working with and <strong>all adjustments are made in terms of the size of that type, not in bytes!<\/strong> (A <code>Void<\/code> pointer does work in terms of bytes since it knows nothing about the size of the type).<\/p>\n<p>[&#8230;]<\/p>\n<p>Why pass in a deallocator function? Technically you might be able to get away with <code>NSData(bytesNoCopy:length:freeWhenDone:)<\/code> but that would just be an accident of implementation. If the Swift runtime did (or does) use a different allocator than the default system <code>malloc\/free<\/code> then You&rsquo;re Gonna Have a Bad Time.<\/p>\n<\/blockquote>\n<p>He doesn&rsquo;t show them, but Swift numbers actually do have built-in methods for endian conversion.<\/p>","protected":false},"excerpt":{"rendered":"<p>Russ Bishop: I want to do this purely in Swift and mostly analogous to what you&rsquo;d do in C because it&rsquo;s relatively quick, doesn&rsquo;t introduce any dependencies, and the decoder is really simple and could be implemented on any platform. [&#8230;] We can calculate the appropriate location in the buffer by performing arithmetic on the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"","apple_news_api_id":"","apple_news_api_modified_at":"","apple_news_api_revision":"","apple_news_api_share_url":"","apple_news_coverimage":0,"apple_news_coverimage_caption":"","apple_news_is_hidden":false,"apple_news_is_paid":false,"apple_news_is_preview":false,"apple_news_is_sponsored":false,"apple_news_maturity_rating":"","apple_news_metadata":"\"\"","apple_news_pullquote":"","apple_news_pullquote_position":"","apple_news_slug":"","apple_news_sections":"\"\"","apple_news_suppress_video_url":false,"apple_news_use_image_component":false,"footnotes":""},"categories":[4],"tags":[571,71,901],"class_list":["post-14535","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-memory-management","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/14535","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/comments?post=14535"}],"version-history":[{"count":3,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/14535\/revisions"}],"predecessor-version":[{"id":14540,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/14535\/revisions\/14540"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=14535"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=14535"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=14535"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}