Focal Point
[SOLVED] HexaDecimal to Decimal Conversion

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

October 16, 2008, 01:26 AM
T.N.V.Pandian
[SOLVED] HexaDecimal to Decimal Conversion
Hi All,
I am using webfocus7.1.3. I need to convert a hexadecimal value to Decimal value.Can anyone help me in this issue.

Thanks in advance.

This message has been edited. Last edited by: Kerry,
October 16, 2008, 02:29 AM
<JG>
Open your documentation and lookup

HEXBYT and BYTVAL
October 16, 2008, 03:04 AM
Tony A
John,

I am not sure that is what TNVP is really looking for? More like a hex string to find the decimal equivalent?

TNVP,

This is a basic maths problem in which you will have to interpret each component of your hex string to obtain the decimal equivalent. You could do that via a simple decode. If you have a max size of the hex string (say x'FF') then you could use an external decode file.

So many ways and methods of doing what I think that you are asking, that really it is best for you to initially analyse your specific requirements and work towards your solution with that in mind.

T



In FOCUS
since 1986
WebFOCUS Server 8.2.01M, thru 8.2.07 on Windows Svr 2008 R2  
WebFOCUS App Studio 8.2.06 standalone on Windows 10 
October 16, 2008, 04:30 AM
nubi
if its a single one-off result you need you can use the google calculator


Developer Studio 7.64
Win XP
Output: mostly HTML, also Excel and PDF

"Never attribute to malice that which can be adequately explained by stupidity." - Heinlein's Razor
October 16, 2008, 04:48 AM
<JG>
Tony, Quite correct, hadn’t had enough coffee.

To make up here's a DM solution.

-SET &HEX = 'ABC123';
-SET &HEXx= &HEX.LENGTH;
-SET &HEXc=1;
-SET &DEC=0;
-REPEAT ENDHEX &HEXx.EVAL TIMES
-SET &HEXa= SUBSTR(&HEX.LENGTH, '&HEX.EVAL', &HEXc, &HEXc, 1, 'A1');
-SET &POWER= &HEX.LENGTH - (&HEXc -1);
-SET &HEXv= DECODE &HEXa('0' 0 '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);
-SET &DECx = (&HEXv * (16 ** (&POWER -1)));
-SET &DEC = &DEC + &DECx ;
-POWER
-SET &HEXc= &HEXc +1;
-ENDHEX
-TYPE &DEC
October 16, 2008, 05:13 AM
Tony A
JG, I only noticed because the match sticks hadn't broken this moring Wink

To complement your DM solution, here's one using a flat file format from my library of useful code (uses autoprompt). It could be used to dump the Hex strings into a temporary table and then pull out the Integer values. If the user enters an invalid character then '0' is substituted.

-SET &Dummy = &Hexstr.Please enter a hex string.;
-* Sting prefixed with 'x' as REVERSE doesn't seem to like pure numerics!
-SET &Hexlen = ARGLEN(999, 'x          &Hexstr.EVAL', 'I3');
-SET &Hexout = UPCASE(10,REVERSE(10,REVERSE(&Hexlen,'x          &Hexstr.EVAL','A&Hexlen.EVAL'),'A10'),'A10');
FILEDEF HEXTOINT DISK HEXTOINT.ftm
-RUN
EX -LINES 5 EDAPUT MASTER,HEXTOINT,CF,MEM,FILENAME=HEXTOINT, SUFFIX=FIX,$
SEGNAME=ONE, SEGTYPE=S0, $
  FIELD=HEXSTRING,     ,A10  ,A10  ,$
SEGNAME=TWO, PARENT=ONE, OCCURS=10, POSITION=HEXSTRING,  SEGTYPE=S, $
  FIELD=HEXCHAR,       ,A1   ,A1   ,$
-RUN
-WRITE HEXTOINT &Hexout
-RUN
TABLE FILE HEXTOINT
PRINT HEXCHAR
      COMPUTE VAL/I3 = IF HEXCHAR FROM 'A' TO 'F' THEN BYTVAL(HEXCHAR, 'I3') - 55
                  ELSE IF HEXCHAR FROM '1' TO '9' THEN BYTVAL(HEXCHAR, 'I3') - 48
                  ELSE 0;
      COMPUTE CNT/I2 = IF HEXSTRING EQ LAST HEXSTRING THEN CNT + 1 ELSE 0;
      COMPUTE POWER/I2 = 9 - CNT;
      COMPUTE HEXINT/D20c = VAL * (16 ** POWER);
   BY HEXSTRING
ON TABLE HOLD
END
-RUN
-*HOLD
TABLE FILE HOLD
SUM HEXINT
 BY HEXSTRING
END
-RUN

T



In FOCUS
since 1986
WebFOCUS Server 8.2.01M, thru 8.2.07 on Windows Svr 2008 R2  
WebFOCUS App Studio 8.2.06 standalone on Windows 10 
October 16, 2008, 05:28 AM
Tony A
Plus a quick example of using it from extracted output -
EX -LINES 5 EDAPUT MASTER,HEXTOINT,CF,MEM,FILENAME=HEXTOINT, SUFFIX=FIX,$
SEGNAME=ONE, SEGTYPE=S0, $
  FIELD=HEXSTRING,     ,A10  ,A10  ,$
SEGNAME=TWO, PARENT=ONE, OCCURS=10, POSITION=HEXSTRING,  SEGTYPE=S, $
  FIELD=HEXCHAR,       ,A1   ,A1   ,$
-RUN
TABLE FILE CAR
BY COUNTRY
ON TABLE SAVE AS HEXTOINT
END
-RUN
TABLE FILE HEXTOINT
PRINT HEXCHAR
      COMPUTE VAL/I3 = IF HEXCHAR FROM 'A' TO 'F' THEN BYTVAL(HEXCHAR, 'I3') - 55
                  ELSE IF HEXCHAR FROM '1' TO '9' THEN BYTVAL(HEXCHAR, 'I3') - 48
                  ELSE 0;
      COMPUTE CNT/I2 = IF HEXSTRING EQ LAST HEXSTRING THEN CNT + 1 ELSE 0;
      COMPUTE POWER/I2 = 9 - CNT;
      COMPUTE HEXINT/D20c = VAL * (16 ** POWER);
   BY HEXSTRING
ON TABLE HOLD
END
-RUN
TABLE FILE HOLD
SUM HEXINT
 BY HEXSTRING
END
-RUN

T



In FOCUS
since 1986
WebFOCUS Server 8.2.01M, thru 8.2.07 on Windows Svr 2008 R2  
WebFOCUS App Studio 8.2.06 standalone on Windows 10 
October 16, 2008, 07:03 AM
FrankDutch
Tony

It's not fair....

You did not even give TNPV the change to work this out after your firts suggestion, which should have been enough.




Frank

prod: WF 7.6.10 platform Windows,
databases: msSQL2000, msSQL2005, RMS, Oracle, Sybase,IE7
test: WF 7.6.10 on the same platform and databases,IE7

October 16, 2008, 08:07 AM
Tony A
Too true Frank, but then he did ask fairly nicely and also JG had given him a suggestion before me. I was just adding to JG's suggestion as a possible TABLE process option Wink Razzer

T



In FOCUS
since 1986
WebFOCUS Server 8.2.01M, thru 8.2.07 on Windows Svr 2008 R2  
WebFOCUS App Studio 8.2.06 standalone on Windows 10 
October 16, 2008, 09:05 AM
Rhonda
Because WebFOCUS has so many manuals, HEXBYT and BYTVAL/BITVAL will be found under functions. If you are using DevStudio the DEFINE tool has Functions will step you through the code.


WebFOCUS & ReportCaster 8.1.05, 7.7.03 - AIX/LINUX, FOCUS 7.6.13 MVS, Output PDF, XLS-XLSX, DOCX and HTML
October 16, 2008, 10:32 AM
T.N.V.Pandian
Thank you JG,T,nubi,Frank,Rhonda for your valuable suggestions.

I have a hexadecimal color code.I want to calculate its RGB equivalent.
e.x. #ffffff to 255 255 255

This is the issue for which i need guidance.

Thanks in advance
October 16, 2008, 10:52 AM
LEX-IA
Is this a one time thing?
Do you need to use it for your style sheet?
is this what you need : http://www.drpeterjones.com/colorcalc/ ?


PROD: WebFOCUS 7.1.0 on Linux/Tomcat 5.5.12 (standalone)/Informix on AIX
TEST: WebFOCUS 7.1.3 on Linux/Tomcat 5.5.16 (standalone)/Informix on AIX
October 16, 2008, 10:57 AM
jimster06
Here are a couple of sources:

http://www.javascripter.net/faq/hextorgb.htm

http://www.easycalculation.com/color-coder.php

http://www.yellowpipe.com/yis/tools/hex-to-rgb/color-converter.php

HTH


jimster06
DevStu WF 7.6.11
W7
HTML, PDF, EXL2K
October 17, 2008, 02:22 AM
<JG>
That is actually a slightly different issue as you are not actually converting
a hex string to decimal you are converting 3 parts of a hex string to three
decimals and to be honest it is much easier to achive.

All you have to do is create a decode table either as a flat file or as inline code
then edit the hex value into its 3 pairs and decode them to the decimal values.
finally combine the decimals to give the RBG

-SET &HEXIN='#ffffff';
-SET &CNTR=0;
-REPEAT ENDREPEAT 3 TIMES
-SET &CNTR = &CNTR +1;
-SET &HEX= IF &CNTR EQ 1 THEN UPCASE(2, EDIT(&HEXIN,'$99'), 'A2') ELSE
- IF &CNTR EQ 2 THEN UPCASE(2, EDIT(&HEXIN,'$$$99'), 'A2') ELSE
- UPCASE(2, EDIT(&HEXIN,'$$$$$99'), 'A2');
-SET &DEC&CNTR.EVAL = DECODE &HEX(
- '00' '00'
.
.
- 'fe' '254'
- 'ff' '255' ELSE '000');
-ENDREPEAT
-SET &RGB = '&DEC1.EVAL' | ' ' | '&DEC2.EVAL' | ' ' | '&DEC3.EVAL';

I'm sure you can fill in decode list.

The same basic code could be put into a define quite easily.
October 17, 2008, 03:05 AM
nubi
quote:
Originally posted by T.N.V.Pandian:
Thank you JG,T,nubi,Frank,Rhonda for your valuable suggestions.

I have a hexadecimal color code.I want to calculate its RGB equivalent.
e.x. #ffffff to 255 255 255

This is the issue for which i need guidance.

Thanks in advance


not a problem i just thought that if it was a one off thing you needed to do its better to use something like google as its designed for quick ad-hoc requests...

the other thing you could download is Paint.net, this is a freeware Photoshop copy but more importantly it has a colour pallete which you can enter Hex or RGB into- so you can get the values that way and actually see the colour at the same time.....


Developer Studio 7.64
Win XP
Output: mostly HTML, also Excel and PDF

"Never attribute to malice that which can be adequately explained by stupidity." - Heinlein's Razor
October 17, 2008, 10:15 AM
Francis Mariani
Here's a Defined function that can be used in Dialogue Manager or WebFOCUS code - it converts one Hex value to one decimal value and can be called multiple time to convert a hex colour value to decimal:

DEFINE FUNCTION HEXTODEC(INHEX/A2)
INHEXA/A1 = EDIT(INHEX, '9$');
INHEXB/A1 = EDIT(INHEX, '$9');

INDECA/I2 =
  IF INHEXA GE '0' AND INHEXA LE '9' THEN EDIT(INHEXA) ELSE DECODE INHEXA ('A' 10, 'B' 11, 'C' 12, 'D' 13, 'E' 14, 'F' 15 ELSE 0);
INDECB/I2 =
  IF INHEXB GE '0' AND INHEXB LE '9' THEN EDIT(INHEXB) ELSE DECODE INHEXB ('A' 10, 'B' 11, 'C' 12, 'D' 13, 'E' 14, 'F' 15 ELSE 0);

HEXTODECN/I3 = (INDECA * 16) + INDECB ;
HEXTODEC/A3  = EDIT(HEXTODECN);
END
-RUN

-SET &COLOUR_HEX = '#F0F9FC';

-SET &COLOUR_DEC =
-  HEXTODEC(EDIT('&COLOUR_HEX.EVAL','$99$$$$')) | ' ' |
-  HEXTODEC(EDIT('&COLOUR_HEX.EVAL','$$$99$$')) | ' ' |
-  HEXTODEC(EDIT('&COLOUR_HEX.EVAL','$$$$$99'));

-TYPE COLOUR &COLOUR_HEX = &COLOUR_DEC




Francis


Give me code, or give me retirement. In FOCUS since 1991

Production: WF 7.7.05M, Dev Studio, BID, MRE, WebSphere, DB2 / Test: WF 8.1.05M, App Studio, BI Portal, Report Caster, jQuery, HighCharts, Apache Tomcat, MS SQL Server
October 17, 2008, 10:49 AM
Francis Mariani
Decimal to Hex defined function:

DEFINE FUNCTION DECTOHEX(INDEC/I3)
INDECD/I2 = INDEC / 16;
INDECR/I2 = IMOD(INDEC, 16, 'I2');

INHEXA/A2 =
  IF INDECD GE 0 AND INDECD LE 9 THEN EDIT(INDECD) ELSE DECODE INDECD (10 ' A', 11 ' B', 12 ' C', 13 ' D', 14 ' E', 15 ' F' ELSE '00');
INHEXB/A2 =
  IF INDECR GE 0 AND INDECR LE 9 THEN EDIT(INDECR) ELSE DECODE INDECR (10 ' A', 11 ' B', 12 ' C', 13 ' D', 14 ' E', 15 ' F' ELSE '00');

DECTOHEX/A2 = EDIT(INHEXA,'$9') | EDIT(INHEXB,'$9');
END
-RUN

-SET &COLOUR_DEC = '240 249 252';

-SET &DEC1 = EDIT('&COLOUR_DEC.EVAL','999$$$$$$$$');
-SET &DEC2 = EDIT('&COLOUR_DEC.EVAL','$$$$999$$$$');
-SET &DEC3 = EDIT('&COLOUR_DEC.EVAL','$$$$$$$$999');

-SET &COLOUR_HEX = '#' | DECTOHEX(&DEC1) | DECTOHEX(&DEC2) | DECTOHEX(&DEC3);

-TYPE COLOUR &COLOUR_DEC = &COLOUR_HEX




Francis


Give me code, or give me retirement. In FOCUS since 1991

Production: WF 7.7.05M, Dev Studio, BID, MRE, WebSphere, DB2 / Test: WF 8.1.05M, App Studio, BI Portal, Report Caster, jQuery, HighCharts, Apache Tomcat, MS SQL Server