cancel
Showing results for 
Search instead for 
Did you mean: 

How Convert the number from hexadecimal to decimal in Crystal XI?

Former Member
0 Kudos

Hi:

I am working on the report with a Invoice ID which is stored in the DB2 database as Varchar(16), the data look like '000000000000000000000000000130D', but the Crystal report need to convert this hexadecimal invoice ID to decimal number, it should look like '4877', actually, most science calculators would have this conversion function. But I canu2019t find this function in Crystal report nor DB2.

I have done some research, the logic hex (16) to decimal is :

since A=10, B=11,C=12,D=13,E=14,F=15

130D= 1X163+3X1620X16^113X16^0=4877

Can some one tell me how to convert the hex number to decimal number in Crystal report?

Thanks!

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi Adelson , I have test my code as below:

***********************************************

Function HEXtodec ( Hex As String) As Number

Dim i As Number

Dim B As Number

Hex = UCase(Hex)

For i = 1 To Len(Hex)

Select Case Mid(Hex, Len(Hex) - i + 1, 1)

Case "0"

B = B + 16 ^ (i - 1) * 0

Case "1"

B = B + 16 ^ (i - 1) * 1

Case "2"

B = B + 16 ^ (i - 1) * 2

Case "3"

B = B + 16 ^ (i - 1) * 3

Case "4"

B = B + 16 ^ (i - 1) * 4

Case "5"

B = B + 16 ^ (i - 1) * 5

Case "6"

B = B + 16 ^ (i - 1) * 6

Case "7"

B = B + 16 ^ (i - 1) * 7

Case "8"

B = B + 16 ^ (i - 1) * 8

Case "9"

B = B + 16 ^ (i - 1) * 9

Case "A"

B = B + 16 ^ (i - 1) * 10

Case "B"

B = B + 16 ^ (i - 1) * 11

Case "C"

B = B + 16 ^ (i - 1) * 12

Case "D"

B = B + 16 ^ (i - 1) * 13

Case "E"

B = B + 16 ^ (i - 1) * 14

Case "F"

B = B + 16 ^ (i - 1) * 15

End Select

Next i

HEXTODEC = B

End Function

*****************************************

but there are something you have to be caution:

1.be sure you choose the VB syntax when you create a New Function in your report.

2.when you use the Function,you can be test such as: hextodec ('01f2')

if you have any problem, contact me free yourself !

Edited by: Hugo Huang on Oct 12, 2008 11:39 AM

Answers (1)

Answers (1)

ido_millet
Active Contributor
0 Kudos

If you are looking for a way to avoid implementing the same logic in a Crystal formula, you can implement this as a UFL (user function library).

Just for the fun of it, I added a Hex2Number() function to my "CUT Light" UFL. I tested this with your string and it indeed returned 4877 in Crystal.

The expression in vb6 is very simple: Val("&H" & strHex) .