<?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: Hexadecimal conversion in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Hexadecimal-conversion/m-p/693352#M211385</link>
    <description>&lt;P&gt;Your code and your description seem to be in conflict.&amp;nbsp; &amp;nbsp;Do you have numeric variables like in the code?&amp;nbsp; Or character string like in the description of the problem?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Post more examples to more clearly show what you are trying to convert.&amp;nbsp; &amp;nbsp;Show what you tried to do and explain how it is not what you want.&lt;/P&gt;</description>
    <pubDate>Thu, 22 Oct 2020 00:17:38 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2020-10-22T00:17:38Z</dc:date>
    <item>
      <title>Hexadecimal conversion</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Hexadecimal-conversion/m-p/693298#M211360</link>
      <description>&lt;P&gt;Hello everyone, I need some help to convert a number which is stored in string (no problem so far) into an hexadecial. Yet no problem, but the thing is, I got convert it back to either a number or a string when it's needed.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Basically what I'm using is:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;	hexa = put(number, hex16.);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;And it aparently it converts but I don't know if it's possible to convert it back.&lt;/P&gt;&lt;P&gt;Thanks in advance.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 21 Oct 2020 20:00:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Hexadecimal-conversion/m-p/693298#M211360</guid>
      <dc:creator>masiqmoselli</dc:creator>
      <dc:date>2020-10-21T20:00:25Z</dc:date>
    </item>
    <item>
      <title>Re: Hexadecimal conversion</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Hexadecimal-conversion/m-p/693307#M211362</link>
      <description>&lt;P&gt;I don't know if there is a function to convert hexadecimal string into it's decimal value. In such case it needs mathematical algorithm:&lt;/P&gt;
&lt;P&gt;1) select each hex digit from right to left and convert it into its decimal value,&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; that is 0 to 15.&lt;/P&gt;
&lt;P&gt;2) multiple it by 15^i where i is the sequential position of the digit counting, right to left, starting with zero.&lt;/P&gt;
&lt;P&gt;3) sum all multiplications.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can create a macro getting the&amp;nbsp;hexadecimal string as an argument.&lt;/P&gt;
&lt;P&gt;Use %length in order to get the string length in bytes.&lt;/P&gt;
&lt;P&gt;Try to create such macro. Post your code, log and results in case of any issue.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 21 Oct 2020 20:23:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Hexadecimal-conversion/m-p/693307#M211362</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2020-10-21T20:23:02Z</dc:date>
    </item>
    <item>
      <title>Re: Hexadecimal conversion</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Hexadecimal-conversion/m-p/693311#M211363</link>
      <description>&lt;P&gt;With width=16, the number is converted to hex as it is stored in 8-byte real format.&lt;/P&gt;
&lt;P&gt;The reciprocal conversion is done when you use the HEX informat with a width of 16. So&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;number = input(hexa,hex16.);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;should get you the original number.&lt;/P&gt;</description>
      <pubDate>Wed, 21 Oct 2020 20:33:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Hexadecimal-conversion/m-p/693311#M211363</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-10-21T20:33:53Z</dc:date>
    </item>
    <item>
      <title>Re: Hexadecimal conversion</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Hexadecimal-conversion/m-p/693352#M211385</link>
      <description>&lt;P&gt;Your code and your description seem to be in conflict.&amp;nbsp; &amp;nbsp;Do you have numeric variables like in the code?&amp;nbsp; Or character string like in the description of the problem?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Post more examples to more clearly show what you are trying to convert.&amp;nbsp; &amp;nbsp;Show what you tried to do and explain how it is not what you want.&lt;/P&gt;</description>
      <pubDate>Thu, 22 Oct 2020 00:17:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Hexadecimal-conversion/m-p/693352#M211385</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-10-22T00:17:38Z</dc:date>
    </item>
    <item>
      <title>Re: Hexadecimal conversion</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Hexadecimal-conversion/m-p/693503#M211471</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/268196"&gt;@masiqmoselli&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You talk about "&lt;SPAN&gt;a number which is stored in string." &lt;/SPAN&gt;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;&lt;SPAN&gt;Is that number an integer?&lt;/SPAN&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;SPAN&gt; How large is it?&lt;/SPAN&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;SPAN&gt; Is it positive or negative?&lt;/SPAN&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;SPAN&gt; What is the purpose of the hexadecimal number you create from the original number?&lt;/SPAN&gt;&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&lt;SPAN&gt;All these questions determine the tools needed for the conversion.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;As &lt;A href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562" target="_blank" rel="noopener"&gt;KurtBremser&lt;/A&gt; has pointed out, there's an important difference between the mathematical decimal-to-hexadecimal conversion (of positive integers) using the &lt;A href="https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.5&amp;amp;docsetId=leforinforref&amp;amp;docsetTarget=n0ueabv26pr2fwn19uxk2f4bf10y.htm&amp;amp;locale=en" target="_blank" rel="noopener"&gt;HEX&lt;EM&gt;w&lt;/EM&gt;. format&lt;/A&gt; with &lt;EM&gt;w&lt;/EM&gt;&amp;lt;=15 and the conversion to 64-bit floating-point format (which SAS uses internally for numeric variables) using the HEX&lt;EM&gt;16&lt;/EM&gt;. format. This is also true for the reverse conversion using the &lt;A href="https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.5&amp;amp;docsetId=leforinforref&amp;amp;docsetTarget=n0vmi0vnlacdocn1oxuu8dpsdztb.htm&amp;amp;locale=en" target="_blank" rel="noopener"&gt;HEX&lt;EM&gt;w&lt;/EM&gt;. informat&lt;/A&gt;.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Here are two examples showing the conversion in both directions:&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Conversion of positive numbers between the decimal system and
   a) the hexadecimal system
   b) 64-bit hexadecimal floating-point representation (Windows)
*/

/* 1) Positive integer (max. 9007199254740992 under Windows) */

data _null_;
dec_int=123; /* hexadecimal: 7B (because 123 = 7*16 + 11) */
hex_int2=put(dec_int, hex2.); /* mathematical decimal-to-hex conversion */
hex_int5=put(dec_int, hex5.); /* same, padded with leading zeros */
hex_float=put(dec_int, hex16.); /* quite different: floating-point representation */
dec_int2=input(hex_int2, hex2.); /* mathematical hex-to-decimal conversion */
dec_int3=input(hex_int5, hex11.); /* any informat length between length(hex_int5) and 16 works */
dec_int4=input(hex_float, hex16.); /* length 16 requires floating-point representation in first argument */
put (_all_)(=);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;Result:&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;dec_int=123 hex_int2=7B hex_int5=0007B hex_float=405EC00000000000 dec_int2=123 dec_int3=123 dec_int4=123&lt;/PRE&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* 2) Positive rational number */

data _null_;
dec_fract=12.3; /* hex: C.4CCC... (12.3 = 12 + 4/16 + 12/16**2 + 12/16**3 + 12/16**4 + ...) */
hex_int5=put(dec_fract, hex5.); /* only the integer part is converted */
hex_float=put(dec_fract, hex16.); /* floating-point representation, but not mathematical notation */
dec_int=input(hex_int5, hex5.); /* no fractional part involved */
dec_fract2=input(hex_float, hex16.); /* conversion yields integer and fractional part */
put (_all_)(=);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;Result:&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;dec_fract=12.3 hex_int5=0000C hex_float=402899999999999A dec_int=12 dec_fract2=12.3&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;If your decimal number is a bit too large for the above standard technique (e.g., an integer with 17-19 digits), see &lt;A href="https://communities.sas.com/t5/SAS-Programming/Converting-a-19-length-number-to-hex-and-then-to-a-string/m-p/653367/highlight/true#M196255" target="_blank" rel="noopener"&gt;this post&lt;/A&gt; for a solution. In the same thread you find a &lt;A href="https://communities.sas.com/t5/SAS-Programming/Converting-a-19-length-number-to-hex-and-then-to-a-string/m-p/653973/highlight/true#M196445" target="_blank" rel="noopener"&gt;macro&lt;/A&gt; for the conversion of positive integers of virtually arbitrary lengths between various number systems (of course including decimal and hexadecimal), an &lt;A href="https://communities.sas.com/t5/SAS-Programming/Converting-a-19-length-number-to-hex-and-then-to-a-string/m-p/654626/highlight/true#M196566" target="_blank" rel="noopener"&gt;FCMP function&lt;/A&gt; for similar conversions of fractions between 0 and 1 and an example of how to apply these two tools in combination to rational numbers &amp;gt;1.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 22 Oct 2020 13:26:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Hexadecimal-conversion/m-p/693503#M211471</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2020-10-22T13:26:48Z</dc:date>
    </item>
  </channel>
</rss>

