Focal Point
[Closed] Hexadecimal to Decimal

This topic can be found at:
https://forums.informationbuilders.com/eve/forums/a/tpc/f/1381057331/m/1947054386

August 16, 2016, 02:33 PM
Avatar
[Closed] Hexadecimal to Decimal
Hello,

I'm looking for some advise on converting Hex to Decimal values.

Something like '073D7AAE' needs to be converted to it's corresponding decimal '121469614'.

I want to perform this as a define or user function instead of creating temporary file for lookup.

Thanks in advance.

This message has been edited. Last edited by: Avatar,


WebFOCUS 8
Windows, All Outputs
August 17, 2016, 08:36 AM
Tamra
Avatar,

The following IBI Techsupport knowledge base link may help you out with this question.

Let us know if it does ( or does not).

How to convert Hex values (A2) to Decimal values (I2)

Thank you for participating in the Focal Point Forum

Kind Regards,
Tamra Colangelo
IBI Focal Point Moderator


WebFOCUS 8x - BI Portal, Developer Studio, App Studio, Excel, PDF, Active Formats and HTML5
August 17, 2016, 08:52 AM
Avatar
Tamra,

I noticed this before. This is merely doing one byte conversion but I want something with more than that.

Eg. 073D7AAE -> 121469614.

I also noted one topic doing this with temp file but I'm looking for something simple, if available.


WebFOCUS 8
Windows, All Outputs
August 17, 2016, 09:44 AM
dbeagan
The code below works for up to four hex positions. You can increase the number of positions as needed. I'm testing it here with PCD from ggsales which has several valid hex values. You may want to implement some kind of error check for invalid values (notice PCD code 'G100' is not valid hex but still shows a result).

 DEFINE FUNCTION hex2dec (hex/A4)
 hexrjust/A4 = RJUST(4,hex,'A4');
 hexchar0/A1 = EDIT(hexrjust,'$$$9'); 
 hexchar1/A1 = EDIT(hexrjust,'$$9'); 
 hexchar2/A1 = EDIT(hexrjust,'$9'); 
 hexchar3/A1 = EDIT(hexrjust,'9');
 hex2dec/P20 =   16**0 * DECODE hexchar0 ('1' 1 '2' 2 '3' 3 '4' 4 '5' 5 '6' 6 '7' 7 '8' 8 '9' 9 'A' 10 'B' 11 'C' 12 'D' 13 'E' 14 'F' 15) 
               + 16**1 * DECODE hexchar1 ('1' 1 '2' 2 '3' 3 '4' 4 '5' 5 '6' 6 '7' 7 '8' 8 '9' 9 'A' 10 'B' 11 'C' 12 'D' 13 'E' 14 'F' 15)  
               + 16**2 * DECODE hexchar2 ('1' 1 '2' 2 '3' 3 '4' 4 '5' 5 '6' 6 '7' 7 '8' 8 '9' 9 'A' 10 'B' 11 'C' 12 'D' 13 'E' 14 'F' 15)  
               + 16**3 * DECODE hexchar3 ('1' 1 '2' 2 '3' 3 '4' 4 '5' 5 '6' 6 '7' 7 '8' 8 '9' 9 'A' 10 'B' 11 'C' 12 'D' 13 'E' 14 'F' 15) 
               ; 
 END
 
 TABLE FILE ibisamp/ggsales
 SUM 
 COMPUTE Result/P20C = hex2dec(PCD);
      BY PCD
 END
 



WebFOCUS 8.2.06
August 17, 2016, 12:06 PM
Hallway
I was able to convert Hex to Dec when using a SQL pass-through. You just need to make sure that you add a '0x' to the front of your HEX as you can see I did in the &SQLHEX variable:

 
-DEFAULTH &MYHEX = '073D7AAE'
-SET &SQLHEX = '0x' | &MYHEX;

ENGINE SQLMSS SET DEFAULT_CONNECTION YOUR_SERVER_NAME
SQL SQLMSS PREPARE SQLOUT FOR

SELECT '&MYHEX' AS HEX, CONVERT(INT, &SQLHEX.EVAL) AS DEC

END

TABLE FILE SQLOUT
PRINT *
END 



I'm sure that you could use the DB_EXPR function if you don't want to use a SQL Pass-through.

This message has been edited. Last edited by: Hallway,


Hallway

 
Prod: 8202M1
Test: 8202M4
Repository:
 
OS:
 
Outputs:
 
 
 
 
August 17, 2016, 08:06 PM
Michael L Meagher
Here's a way to do it in Dialogue Manager:
-* HexToDec - Hexadecimal to Decimal Conversion
-* Michael Meagher
-* DataMine, LLC

-SET &N=1;
-SET &HexArray.&N = '1';
-SET &N=&N+1;
-SET &HexArray.&N = '2';
-SET &N=&N+1;
-SET &HexArray.&N = '3';
-SET &N=&N+1;
-SET &HexArray.&N = '4';
-SET &N=&N+1;
-SET &HexArray.&N = '5';
-SET &N=&N+1;
-SET &HexArray.&N = '6';
-SET &N=&N+1;
-SET &HexArray.&N = '7';
-SET &N=&N+1;
-SET &HexArray.&N = '8';
-SET &N=&N+1;
-SET &HexArray.&N = '9';
-SET &N=&N+1;
-SET &HexArray.&N = 'A';
-SET &N=&N+1;
-SET &HexArray.&N = 'B';
-SET &N=&N+1;
-SET &HexArray.&N = 'C';
-SET &N=&N+1;
-SET &HexArray.&N = 'D';
-SET &N=&N+1;
-SET &HexArray.&N = 'E';
-SET &N=&N+1;
-SET &HexArray.&N = 'F';
-SET &N=&N+1;
-SET &HexArray.&N = 'G';

-SET &HexLength = &Hexadecimal.LENGTH;
-SET &LengthFormat = 'A'||&HexLength;
-SET &Hexadecimal = UPCASE(&HexLength,&Hexadecimal,'&LengthFormat');
-SET &J=0;
-SET &Decimal=0;

-ParseLoop
-* Parse the hexadecimal string
-* &J will be the position of &HexChar
-IF &J GT &HexLength THEN GOTO Done;
-SET &J=&J+1;
-SET &HexChar=SUBSTR(&HexLength, &Hexadecimal, &J, &J, 1, 'A1');
-SET &I=0;

-PowerSearch
-* Assign an integer value to &HexChar
-* &I will be the decimal representation of &HexChar.
-* For example &I will be 1 when HexChar is 1, and &I will be 14 when HexChar is E.
-SET &I=&I+1;
-IF &I GT 16 THEN GOTO ParseLoop;
-IF &HexChar EQ &HexArray.&I GOTO BuildDecimal;
-GOTO PowerSearch

-BuildDecimal
-* To get the actual decimal value for &HexChar we raise 16 to the power of (&HexLength-&J) 
-* and multiply by &I. Then add the results to the previous total.
-* 
-SET &Power = &HexLength-&J;
-SET &Decimal = &Decimal + &I*(16**&Power);
-GOTO ParseLoop

-Done
-TYPE &Hexadecimal
-TYPE &Decimal



WebFOCUS 8.2.03 - Production
WebFOCUS 8.2.04 - Sand Box
Windows 2012 R2 Server
HTML, PDF, Excel
In FOCUS since 1980
August 18, 2016, 10:42 AM
Avatar
Thanks to all for your help and your time ...

I was able to accomplish what I wanted with dbeagan's suggestion and special thanks to dbeagan

Cheers...


WebFOCUS 8
Windows, All Outputs