{"id":1233,"date":"2012-09-20T09:31:35","date_gmt":"2012-09-19T21:31:35","guid":{"rendered":"https:\/\/www.deltics.co.nz\/blog\/?p=1233"},"modified":"2012-09-20T09:31:35","modified_gmt":"2012-09-19T21:31:35","slug":"reversebytes","status":"publish","type":"post","link":"https:\/\/www.deltics.co.nz\/blog\/posts\/1233\/","title":{"rendered":"ReverseBytes()"},"content":{"rendered":"<span class=\"span-reading-time rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\">[Estimated Reading Time: <\/span> <span class=\"rt-time\"> 2<\/span> <span class=\"rt-label rt-postfix\">minutes]<\/span><\/span><p>In some <a href=\"http:\/\/stackoverflow.com\/questions\/1482699\/zeroconf-bonjour-code-that-works-in-delphi-7-not-working-in-2009\/3527955#comment16780848_3527955\" target=\"_blank\">comments on StackOverflow<\/a>, Jeroen asked me to post my code for reversing bytes.  Rather than posting code into that question\/answer that wasn&#8217;t directly relevant to the question\/answer, I decided to quickly throw the code up on here.<\/p>\n<p><!--more--><\/p>\n<p>The intent with <strong>ReverseBytes()<\/strong> is &#8211; as the name says &#8211; to reverse the byte-order in some byte-order significant value.  This sort of operation is typically required when dealing with values in data originating from systems that have a different &#8220;endianess&#8221; than the Intel x86 architecture.<\/p>\n<p>This most often means on some <strong>word<\/strong> or <strong>long word<\/strong> sized value (and potentially a huge word, though I&#8217;ve not yet encountered that need hence no overload yet exists for that &#8211; yet.  I should probably add one just in case \ud83d\ude42 ).<\/p>\n<p>Anyway, here are my current implementations of this routine:<\/p>\n<pre class=\"brush: delphi; title: ; notranslate\" title=\"\">\r\ninterface\r\n\r\n  function ReverseBytes(const aValue: Word): Word; overload;\r\n  function ReverseBytes(const aValue: LongWord): LongWord; overload;\r\n\r\n\r\nimplementation\r\n\r\n  { - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - }\r\n  function ReverseBytes(const aValue: Word): Word;\r\n  begin\r\n    result :=  (((aValue and $ff00) shr 8)\r\n             or ((aValue and $00ff) shl 8));\r\n  end;\r\n\r\n\r\n  { - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - }\r\n  function ReverseBytes(const aValue: LongWord): LongWord;\r\n  begin\r\n    result :=  (((aValue and $ff000000) shr 24)\r\n            or  ((aValue and $00ff0000) shr 8)\r\n            or  ((aValue and $0000ff00) shl 8)\r\n            or  ((aValue and $000000ff) shl 24));\r\n  end;\r\n<\/pre>\n<p>And, by way of sneak preview, here are the unit tests expressed in my Smoketest framework that test these routines:<\/p>\n<pre class=\"brush: delphi; title: ; notranslate\" title=\"\">\r\n  procedure TUnitTest_SysUtils.fn_ReverseBytes;\r\n  const\r\n    W_init  = $0102;\r\n    DW_init = $01020304;\r\n    W_reversed  = $0201;\r\n    DW_reversed  = $04030201;\r\n  begin\r\n    Test('ReverseBytes(word)!').Expect(ReverseBytes(W_init)).Equals(W_reversed);\r\n    Test('ReverseBytes(long word)!').Expect(ReverseBytes(DW_init)).Equals(DW_reversed);\r\n    Test('ReverseBytes(ReverseBytes(word))!').Expect(ReverseBytes(ReverseBytes(W_init))).Equals(W_init);\r\n    Test('ReverseBytes(ReverseBytes(long word))!').Expect(ReverseBytes(ReverseBytes(DW_init))).Equals(DW_init);\r\n  end;\r\n<\/pre>\n<p>The Smoketest code is no use without the Smoketest framework itself, obviously, and is provided here as I say only as a sneak preview as I hope and intend to have Smoketest in a releasable form soon.<\/p>\n<p>A slight wrinkle in those plans was the discovery of the <a href=\"https:\/\/www.deltics.co.nz\/blog\/?p=1218\" target=\"_blank\">lack of support for dotted unit names in [the current &#8220;official&#8221; release of] FPC, 2.6.x<\/a>.<\/p>\n<p>I was considering removing the dots from my unit names to address this, but am increasingly of a mind to leave them in.  This will mean releasing for Delphi initially and simply waiting for FPC to catch up in this area (dotted unit name support is in 2.7 already, in preview form at least).<\/p>\n","protected":false},"excerpt":{"rendered":"<p><span class=\"span-reading-time rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\">[Estimated Reading Time: <\/span> <span class=\"rt-time\"> 2<\/span> <span class=\"rt-label rt-postfix\">minutes]<\/span><\/span>In some comments on StackOverflow, Jeroen asked me to post my code for reversing bytes. Rather than posting code into that question\/answer that wasn&#8217;t directly relevant to the question\/answer, I decided to quickly throw the code up on here.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[4],"tags":[186,292],"class_list":["post-1233","post","type-post","status-publish","format-standard","hentry","category-delphi","tag-biglittle-endianess","tag-delphi"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p1TKYv-jT","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/posts\/1233","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/comments?post=1233"}],"version-history":[{"count":2,"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/posts\/1233\/revisions"}],"predecessor-version":[{"id":1235,"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/posts\/1233\/revisions\/1235"}],"wp:attachment":[{"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/media?parent=1233"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/categories?post=1233"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/tags?post=1233"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}