Convert Int to Hex and vice versa with DotNet

okioki Member Posts: 46
You can use DotNet functionality to do the job for you.

1.) Variable Declarations used in the following examples:
Name      DataType Subtype
LInt32    DotNet   System.Int32.'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'	
LInt64    DotNet   System.Int64.'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'	
LIntP     DotNet   System.IntPtr.'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'	
NumStyles DotNet   System.Globalization.NumberStyles.'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'	
i	  Integer		
bi	  BigInteger	
s	  Text

2.) Conversion from Integer to Hexadecimal
Because System.Int32 and System.Int64 variables cannot neither be assigned a value nor constructed in CA/L, the System.IntPtr must be used:
// let LIntP point on value or variable, where the variable can be Integer or BigInteger
LIntP := LIntP.IntPtr(<int value or CA/L-int variable>); 

// let s get the hexadecimal representaion of the integer above, filled with preceeding zeroes if hex-number has less than 4 digits
s := LIntP.ToString('X4');

MESSAGE('Int To Hex:\Int-Value='+LIntP.ToString+', Hex-Value='+s);

3.) Conversion from Integer to Hexadecimal
Here we can use the System.Int32 and System.Int64 because we only use the Parse method.
Little watch is needed here using the right size type.
s := '1DC942DE';
i := LInt32.Parse(s, NumStyles.HexNumber);
MESSAGE('Hex To Int (32bit)\Hex-Value=%1, Int-Value=%2',s,i);

s := 'FDC96EDBFFFFFFFF';
bi := LInt64.Parse(s, NumStyles.HexNumber);
MESSAGE('Hex To Int (64bit)\Hex-Value=%1, Int-Value=%2',s,bi);

Have fun :smile:
Sign In or Register to comment.