BookmarkSubscribeRSS Feed
masiqmoselli
Obsidian | Level 7

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. 

Basically what I'm using is:

 

	hexa = put(number, hex16.);

And it aparently it converts but I don't know if it's possible to convert it back.

Thanks in advance. 

 

4 REPLIES 4
Shmuel
Garnet | Level 18

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:

1) select each hex digit from right to left and convert it into its decimal value,

    that is 0 to 15.

2) multiple it by 15^i where i is the sequential position of the digit counting, right to left, starting with zero.

3) sum all multiplications.

 

You can create a macro getting the hexadecimal string as an argument.

Use %length in order to get the string length in bytes.

Try to create such macro. Post your code, log and results in case of any issue. 

Kurt_Bremser
Super User

With width=16, the number is converted to hex as it is stored in 8-byte real format.

The reciprocal conversion is done when you use the HEX informat with a width of 16. So

number = input(hexa,hex16.);

should get you the original number.

Tom
Super User Tom
Super User

Your code and your description seem to be in conflict.   Do you have numeric variables like in the code?  Or character string like in the description of the problem?

 

Post more examples to more clearly show what you are trying to convert.   Show what you tried to do and explain how it is not what you want.

FreelanceReinh
Jade | Level 19

Hello @masiqmoselli,

 

You talk about "a number which is stored in string."

  1. Is that number an integer?
  2. How large is it?
  3. Is it positive or negative?
  4. What is the purpose of the hexadecimal number you create from the original number?

All these questions determine the tools needed for the conversion.

 

As KurtBremser has pointed out, there's an important difference between the mathematical decimal-to-hexadecimal conversion (of positive integers) using the HEXw. format with w<=15 and the conversion to 64-bit floating-point format (which SAS uses internally for numeric variables) using the HEX16. format. This is also true for the reverse conversion using the HEXw. informat.

 

Here are two examples showing the conversion in both directions:

/* 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;

Result:

dec_int=123 hex_int2=7B hex_int5=0007B hex_float=405EC00000000000 dec_int2=123 dec_int3=123 dec_int4=123
/* 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;

Result:

dec_fract=12.3 hex_int5=0000C hex_float=402899999999999A dec_int=12 dec_fract2=12.3

If your decimal number is a bit too large for the above standard technique (e.g., an integer with 17-19 digits), see this post for a solution. In the same thread you find a macro for the conversion of positive integers of virtually arbitrary lengths between various number systems (of course including decimal and hexadecimal), an FCMP function for similar conversions of fractions between 0 and 1 and an example of how to apply these two tools in combination to rational numbers >1.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 2386 views
  • 1 like
  • 5 in conversation