{"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=\"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=\"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_post_was_ever_published":false,"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":[]},"categories":[4],"tags":[186,292],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p1TKYv-jT","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":2337,"url":"https:\/\/www.deltics.co.nz\/blog\/posts\/2337\/","url_meta":{"origin":1233,"position":0},"title":"A Silent Danger&#8230;","date":"07 Jun 2015","format":false,"excerpt":"A brief post on a long standing omission in type checking in Pascal and the limitations of Range Checking as applied to the problem. Consider this contrived example of a simple function: This very simple function accepts an explicitly 32-bit Integer parameter and simply returns TRUE if the value passed\u2026","rel":"","context":"In &quot;Delphi&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":338,"url":"https:\/\/www.deltics.co.nz\/blog\/posts\/338\/","url_meta":{"origin":1233,"position":1},"title":"Delphi 2009 &#8211; A Heads-Up for Low-Level Coders","date":"13 Sep 2008","format":false,"excerpt":"Prompted by a conversation with some colleagues where-in we collectively speculated about the implementation details of a generic class and what impact - if any - this might have on performance vs a \"traditional\" polymorphic equivalent, I threw together a quick performance test case in my Smoketest framework, and as\u2026","rel":"","context":"In &quot;Delphi&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2597,"url":"https:\/\/www.deltics.co.nz\/blog\/posts\/2597\/","url_meta":{"origin":1233,"position":2},"title":"(True = 1) and (True = &#8216;-1&#8217;) ?","date":"17 Mar 2017","format":false,"excerpt":"It has been observed that the Delphi documentation states that the constants True and False have the values 1 and 0 respectively, not the -1 and 0 that the default string conversions apply. This does actually make sense but also lays a trap for the unwary. True, True, Wherefore Art\u2026","rel":"","context":"In &quot;Delphi&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":254,"url":"https:\/\/www.deltics.co.nz\/blog\/posts\/254\/","url_meta":{"origin":1233,"position":3},"title":"An Exchange() For All","date":"26 Aug 2008","format":false,"excerpt":"Following on from yesterday's post, Barry Kelly (CodeGear engineer) kindly clarified a few points, one of which was that Generics support in Delphi 2009 won't extent to unit procedures, only class methods, so speculation about a possible generic implementation of a Swap()\/Exchange() routine was rendered largely academic. Not to be\u2026","rel":"","context":"In &quot;Delphi&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1240,"url":"https:\/\/www.deltics.co.nz\/blog\/posts\/1240\/","url_meta":{"origin":1233,"position":4},"title":"What&#8217;s in a Word &#8230; ?","date":"21 Sep 2012","format":false,"excerpt":"In an exchange with David Heffernan both on SO and in the comments here on Te Waka, I had cause to climb in my own personal \"Wayback Machine\" and further investigate an apparent change in compiler behaviour between Delphi 2007 and 2009. This change was first identified as the result\u2026","rel":"","context":"In &quot;Delphi&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":507,"url":"https:\/\/www.deltics.co.nz\/blog\/posts\/507\/","url_meta":{"origin":1233,"position":5},"title":"Mind the Gap(s)","date":"14 Sep 2009","format":false,"excerpt":"This is what I love about Delphi. \u00a0After almost 15 years of Delphi'ing there's still new things to learn, and I don't just mean new features in the latest releases. \u00a0I mean, basic, fundamental things that have been there for years, just undiscovered (by me). \u00a0In this case the discovery\u2026","rel":"","context":"In &quot;Delphi&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/posts\/1233"}],"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}]}}