<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Byte swapping in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Byte-swapping/m-p/838861#M36276</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/426087"&gt;@linlin87&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thanks mkeintz for explaining, very useful. How do I pass a character string to this process? For example I have the character variable &lt;CODE class=" language-sas"&gt;value&lt;/CODE&gt;&amp;nbsp;which takes values like&amp;nbsp;&lt;CODE class=" language-sas"&gt;80f8d501&lt;/CODE&gt;. How do I pass this rather than the txt= example you gave above?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data input;
set input;
put value;
z=input(value,ib4.);
put value= value=hex8. z=;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and the log reads for the first row of data&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;80f8d501
value=80f8d501 value=38306638 z=946221112&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;I was expecting z to read&amp;nbsp;30800000.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Any help would be great!&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Your value variable is apparently eight bytes long (value='80f8d501').&amp;nbsp; &amp;nbsp;This is equivalent to 16&amp;nbsp;&lt;A href="https://en.wikipedia.org/wiki/Nibble" target="_self"&gt;nibble'&lt;/A&gt;s, which have a one-to-one correspondence with hexadecimal digits.&amp;nbsp; Using the IB4 informat in the input function reads the first 4 bytes, i.e. the first 8 nibbles.&amp;nbsp; So for your situation, it is converting hexadecimal 38306638 to an integer, not hexadecimal 80f8d501.&amp;nbsp; &amp;nbsp;For instance, the first character '8' is stored as '38'x, which is '00110110' binary.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;806  data _null_;
807    value='80f8d501';   /* 8 bytes long */
808    put value=hex16.;   /* Write 8 bytes as 16 nibbles */
809  run;

value=3830663864353031
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So if you can't go back and read in VALUE as a 4-character (i.e. 4 bytes = 8 nibbles) variable, then you need to create an intermediate four-byte variable which is truly '80f8d501' hexadecimal:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;860  data _null_;
861    value='80f8d501';   /* 8 bytes long */
862    put value=  ' equivalent in hexadecimal to ' value hex16. ;
863
864    length hexvalue $4;
865    hexvalue=input(value,$hex8.);
866    z=input(hexvalue,ib4.);
867    put hexvalue=hex8. z=;
868  run;

value=80f8d501  equivalent in hexadecimal to 3830663864353031
hexvalue=80F8D501 z=30800000
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sun, 16 Oct 2022 10:46:21 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2022-10-16T10:46:21Z</dc:date>
    <item>
      <title>Byte swapping</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Byte-swapping/m-p/838837#M36267</link>
      <description>&lt;P&gt;Hi SAS Communities&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;I have a question. I have a hexidecimal value (for example 20 e9 ef 01&lt;SPAN&gt;), but I need to convert this to little endian format first (byte swapping) before I can get the decimal number. For this examples it would be that&amp;nbsp;hexadecimal&amp;nbsp;20 e9 ef 01 is converted to little endian of 01 EF E9 20 which can then be converted to integer of&amp;nbsp;32500000 (UINT32).&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;How do I do this process in SAS?&lt;BR /&gt;&lt;BR /&gt;I have following&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data test;
set indata;
format tab hex8.;
lit_end=input(tab,$UCS2LEw.);
run;
&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;I assumed simply applying new informat of little endian would convert hexidecimal to little endian format. But it show:&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&lt;BR /&gt;&lt;/SPAN&gt;ERROR 48-59: The informat UCS2LEW was not found or could not be loaded.&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;I wonder if other way to do?&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Any help would be very great.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 15 Oct 2022 21:55:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Byte-swapping/m-p/838837#M36267</guid>
      <dc:creator>linlin87</dc:creator>
      <dc:date>2022-10-15T21:55:14Z</dc:date>
    </item>
    <item>
      <title>Re: Byte swapping</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Byte-swapping/m-p/838845#M36268</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/426087"&gt;@linlin87&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi SAS Communities&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;I have a question. I have a hexidecimal value (for example 20 e9 ef 01&lt;SPAN&gt;), but I need to convert this to little endian format first (byte swapping) before I can get the decimal number. For this examples it would be that&amp;nbsp;hexadecimal&amp;nbsp;20 e9 ef 01 is converted to little endian of 01 EF E9 20 which can then be converted to integer of&amp;nbsp;32500000 (UINT32).&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;How do I do this process in SAS?&lt;/SPAN&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;Any big endian hexadecimal number that ends in "01"&amp;nbsp; (such as 20e9ef01) cannot generate an even integer value (such as 32500000).&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;But o&lt;/SPAN&gt;&lt;SPAN&gt;n my little-Endian intel machine, the hexadecimal value&amp;nbsp;20E9Ef01 becomes 32500000 using the IB4.&amp;nbsp; format.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;708  data _null_;
709    length txt $4;
710    txt='20E9Ef01'x;
711    z=input(txt,ib4.);
712    put txt=$hex8. z=;
713  run;

txt=20E9EF01 z=32500000
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;In other words, your initial value appears to be little endian already.&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Now if your data really does start out as big endian, and you are processing it on a little endian machine, then&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;778  data _null_;
779    length bigendian $4;
780    bigendian='01EFE920'x;
781    ZBIG=input(bigendian,s370fpib4.);
782    put bigendian=$hex8. ZBIG=;
783  run;

bigendian=01EFE920 ZBIG=32500000
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;Take a look at&amp;nbsp;&lt;A title="Byte Ordering for Integer Binary Data on Big Endian and Little Endian Platforms" href="https://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a001258728.htm" target="_self"&gt;Byte Ordering for Integer Binary Data on Big Endian and Little Endian Platforms&lt;/A&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As you can see, I assume you don't really need to reverse byte order, but you do need to know want informat to use to read the data given its "endian" attribute.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 16 Oct 2022 01:56:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Byte-swapping/m-p/838845#M36268</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2022-10-16T01:56:06Z</dc:date>
    </item>
    <item>
      <title>Re: Byte swapping</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Byte-swapping/m-p/838858#M36274</link>
      <description>&lt;P&gt;Thanks mkeintz for explaining, very useful. How do I pass a character string to this process? For example I have the character variable &lt;CODE class=" language-sas"&gt;value&lt;/CODE&gt;&amp;nbsp;which takes values like&amp;nbsp;&lt;CODE class=" language-sas"&gt;80f8d501&lt;/CODE&gt;. How do I pass this rather than the txt= example you gave above?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data input;
set input;
put value;
z=input(value,ib4.);
put value= value=hex8. z=;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and the log reads for the first row of data&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;80f8d501
value=80f8d501 value=38306638 z=946221112&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;I was expecting z to read&amp;nbsp;30800000.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Any help would be great!&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 16 Oct 2022 08:51:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Byte-swapping/m-p/838858#M36274</guid>
      <dc:creator>linlin87</dc:creator>
      <dc:date>2022-10-16T08:51:33Z</dc:date>
    </item>
    <item>
      <title>Re: Byte swapping</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Byte-swapping/m-p/838861#M36276</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/426087"&gt;@linlin87&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thanks mkeintz for explaining, very useful. How do I pass a character string to this process? For example I have the character variable &lt;CODE class=" language-sas"&gt;value&lt;/CODE&gt;&amp;nbsp;which takes values like&amp;nbsp;&lt;CODE class=" language-sas"&gt;80f8d501&lt;/CODE&gt;. How do I pass this rather than the txt= example you gave above?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data input;
set input;
put value;
z=input(value,ib4.);
put value= value=hex8. z=;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and the log reads for the first row of data&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;80f8d501
value=80f8d501 value=38306638 z=946221112&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;I was expecting z to read&amp;nbsp;30800000.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Any help would be great!&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Your value variable is apparently eight bytes long (value='80f8d501').&amp;nbsp; &amp;nbsp;This is equivalent to 16&amp;nbsp;&lt;A href="https://en.wikipedia.org/wiki/Nibble" target="_self"&gt;nibble'&lt;/A&gt;s, which have a one-to-one correspondence with hexadecimal digits.&amp;nbsp; Using the IB4 informat in the input function reads the first 4 bytes, i.e. the first 8 nibbles.&amp;nbsp; So for your situation, it is converting hexadecimal 38306638 to an integer, not hexadecimal 80f8d501.&amp;nbsp; &amp;nbsp;For instance, the first character '8' is stored as '38'x, which is '00110110' binary.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;806  data _null_;
807    value='80f8d501';   /* 8 bytes long */
808    put value=hex16.;   /* Write 8 bytes as 16 nibbles */
809  run;

value=3830663864353031
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So if you can't go back and read in VALUE as a 4-character (i.e. 4 bytes = 8 nibbles) variable, then you need to create an intermediate four-byte variable which is truly '80f8d501' hexadecimal:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;860  data _null_;
861    value='80f8d501';   /* 8 bytes long */
862    put value=  ' equivalent in hexadecimal to ' value hex16. ;
863
864    length hexvalue $4;
865    hexvalue=input(value,$hex8.);
866    z=input(hexvalue,ib4.);
867    put hexvalue=hex8. z=;
868  run;

value=80f8d501  equivalent in hexadecimal to 3830663864353031
hexvalue=80F8D501 z=30800000
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 16 Oct 2022 10:46:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Byte-swapping/m-p/838861#M36276</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2022-10-16T10:46:21Z</dc:date>
    </item>
    <item>
      <title>Re: Byte swapping</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Byte-swapping/m-p/838879#M36279</link>
      <description>&lt;P&gt;If the value is already encoded into a hex string then use the $HEX informat to convert the hex string back into an actual string.&lt;/P&gt;
&lt;PRE&gt;55    data _null_;
56      txt='80f8d501';
57      x=input(input(txt,$hex8.),ib4.);
58      put txt = x=comma20.;
59   run;

txt=80f8d501 x=30,800,000
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds

&lt;/PRE&gt;</description>
      <pubDate>Sun, 16 Oct 2022 23:14:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Byte-swapping/m-p/838879#M36279</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-10-16T23:14:16Z</dc:date>
    </item>
  </channel>
</rss>

