Hello @masiqmoselli,
You talk about "a number which is stored in string."
Is that number an integer?
How large is it?
Is it positive or negative?
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.
... View more