Let's Get Social!
Focal Point    Focal Point Forums    iWay Software Product Forum on Focal Point    [Closed] Hexadecimal to Decimal
 Go New Search Notify Tools Reply
Silver Member
 posted August 16, 2016 02:33 PM
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.

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

WebFOCUS 8
Windows, All Outputs

 Posts: 38 | Registered: July 25, 2016 IP
Guru
 posted August 17, 2016 08:36 AM Hide Post
Avatar,

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

 Posts: 487 | Location: Toronto | Registered: June 23, 2009 IP
Silver Member
 posted August 17, 2016 08:52 AM Hide Post
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

 Posts: 38 | Registered: July 25, 2016 IP
Platinum Member
 posted August 17, 2016 09:44 AM Hide Post
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
```

WF 8.2.03
Win10 / IE11
AHTML EXL2K PDF COMT WP ZIP

 Posts: 134 | Registered: October 19, 2010 IP
Guru
 posted August 17, 2016 12:06 PM Hide Post
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 WF(Prod): 8202M WF(Test): 8202M OS/Platform: Win 10 Outputs: All

 Posts: 363 | Location: Salt Lake City, UT, USA | Registered: November 18, 2015 IP
Platinum Member
 posted August 17, 2016 08:06 PM Hide Post
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 &LengthFormat = 'A'||&HexLength;
-SET &J=0;
-SET &Decimal=0;

-ParseLoop
-* &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 &Decimal
```

WebFOCUS 8.2.03 - Production
WebFOCUS 8.2.04 - Sand Box
Windows 2012 R2 Server
HTML, PDF, Excel
In FOCUS since 1980

 Posts: 116 | Location: Seattle, WA | Registered: April 07, 2015 IP
Silver Member
 posted August 18, 2016 10:42 AM Hide Post

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

Cheers...

WebFOCUS 8
Windows, All Outputs

 Posts: 38 | Registered: July 25, 2016 IP