Link to home
Start Free TrialLog in
Avatar of gusdarino
gusdarino

asked on

Formula to convert Hexadecimal to decimal numbers and Decimal numbert to Hexadecimal

I am looking for a function or formula to convert hexadecimal numbers to decimal and Decimal numbert to Hexadecimal
In Excel I use the "HEX2DEC" and "DEC2HEX" functions but I don't see any similar functions in Crystal.

Sample:
Value in hexadecimal Value in decimal
0x0001      1
0x0002      2
0x0008      8
0x0010      16
0x0020      32
0x0040      64
0x0080      128
0x0100      256
0x0200      512
0x0800      2048
0x1000      4096
0x2000      8192
0x10000      65536
0x20000      131072
0x40000      262144
0x80000      524288
0x100000      1048576
0x200000      2097152
0x400000      4194304
0x800000      8388608
0x1000000      16777216

If I have a decimal number of 66050 the Hexdecimal equivalent is  10202

I am using Crystal Pro 10.  My datasource is a CSV file.

Thanks,

Gus Darino
ASKER CERTIFIED SOLUTION
Avatar of frodoman
frodoman
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This formula takes you the other way - from decimal to hex.  As far as I know there's no built in Crystal function so you'll have to use your own formulas to do the calculations.

  stringVar result;
  numberVar digit;
  numberVar decval;
  numberVar hexval;
  stringVar hvalue;

  decval := {table.field};
  for digit := 10 to 1 step -1 do
  (
     hexval := int( decval / (16 ^ digit));
     decval := decval - (hexval * ( 16 ^ digit ));
     
     if hexval < 10 then
        hvalue := cstr(hexval)
     else
     (
        select hexval
           case 10: hvalue := 'A'
           case 11: hvalue := 'B'
           case 12: hvalue := 'C'
           case 13: hvalue := 'D'
           case 14: hvalue := 'E'
           case 15: hvalue := 'F'
      );    
      result := result + hvalue;          
  );
  result;

Note that this formula has a hard coded 10 digit result so you'll always have 10 digits (with padded zeros as needed).  If you don't want the extra zeros on the front then change this line:

if hexval < 10 then

to this

if hexval < 10 and hexval > 0 then

and then in the case statement add a case to handle zero:  case 0: hvalue := '';  

HTH

frodoman
Avatar of gusdarino
gusdarino

ASKER

frodoman

Thank you for your help!

The formula to convert from Hex to Dec works fine.  
There is a problem with the formula to convert from Dec to Hex.  If the reference value is 514 the Hex should be 0202 but I get 0020 wich is hex for dec 32.  If i enter 512 I get 0020 which is wrong as well.

Gus
Sorry - I converted this from an Oracle function that I'd written and I made a translation error.  Change these two lines:

     hexval := int( decval / (16 ^ digit));
     decval := decval - (hexval * ( 16 ^ digit ));

To this:

     hexval := int( decval / (16 ^ (digit-1)));
     decval := decval - (hexval * ( 16 ^ (digit-1)));

That should get rid of the shifted values.

frodoman
Works like a charm!

Thank you very much!

Gus Darino
Thanks, I just got through a 3 month process of evaluating different report and BI solutions for my company so I had it fresh in my mind.  :-)
Glad to help - frodoman