Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


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

Posted on 2004-10-21
Medium Priority
Last Modified: 2012-08-14
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.

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.


Gus Darino
Question by:gusdarino
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
LVL 42

Accepted Solution

frodoman earned 2000 total points
ID: 12372218
This formula converts a Hex string parameter (RefNumber) to a decimal value:

// Hex To Dec
numberVar x := len({?RefNumber});
numberVar base16 := 1;
numberVar value := 0;
numberVar decval := 0;
stringVar s;

while x > 0 do
    s := mid({?RefNumber},x,1);
    if s = "F" then
        decval := 15
    else if s = "E" then
        decval := 14
    else if s = "D" then
        decval := 13
    else if s = "C" then
        decval := 12
    else if s = "B" then
        decval := 11
    else if s = "A" then
        decval := 10
        decval := cdbl(s);

    value := value + (base16 * decval);
    base16 := base16 * 16;
    x := x - 1;



LVL 42

Expert Comment

ID: 12372349
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)
        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;          

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 := '';  



Author Comment

ID: 12372814

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.

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

LVL 42

Expert Comment

ID: 12373126
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.

LVL 42

Expert Comment

ID: 12373138

Author Comment

ID: 12373408
Works like a charm!

Thank you very much!

Gus Darino

Author Comment

ID: 12373504
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.  :-)
LVL 42

Expert Comment

ID: 12373624
Glad to help - frodoman

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Crystal Reports: 5 Tests for Top Performance It is complete, your masterpiece report.  Not only does it meet your customer’s expectations, it blows them out the water, all they want is beautifully summarised and displayed in a myriad of ways. …
There have always been a lot of questions related to when Crystal Reports evaluates report components (such as formulas, summaries, cross-tabs, charts, to name a few examples). Crystal Reports uses a two-pass reporting process to provide greater …
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question