McBeen.com

Crystal Reports Hex Conversion

by Danlor on Apr.19, 2005, under Technology

So… your in Crystal, working with a database that decided it would be a GREAT idea to store all integer values as hex strings… Just use a hex conversion command right? Oh wait… there isn’t one… CRAP.

You can now rest easy knowing I have created a flexible conversion script in crystal syntax with no external modules. Just substitue your field for conversion in the top declaration. This script takes for granted that the field is flagged with 0x in the front. If you want to convert raw hex strings, then set line 3 to local stringvar hex_val:=hex_val.


Local stringvar hex_in :={your field to be converted};
if len(hex_in)>0 then
Local stringvar hex_val :=right(hex_in,len(hex_in)-2);
Local NumberVar strLen := Length (hex_val);
Local NumberVar i;
Local NumberVar result;
For i := 0 To strLen-1 Do
(
select (mid (hex_val,i+1,1))
case "a" : result := result+10*(16 ^ (len(hex_val)-(i+1)))
case "b" : result := result+11*(16 ^ (len(hex_val)-(i+1)))
case "c" : result := result+12*(16 ^ (len(hex_val)-(i+1)))
case "d" : result := result+13*(16 ^ (len(hex_val)-(i+1)))
case "e" : result := result+14*(16 ^ (len(hex_val)-(i+1)))
case "f" : result := result+15*(16 ^ (len(hex_val)-(i+1)))
default : result := result+val(mid (hex_val,i+1,1))*(16 ^ (len(hex_val)-(i+1)))
);
result;

:

4 Comments for this entry

  • Len Alexandropoulos

    How do you convert a Hexadecimal to a decimal in Crystal Reports.

  • admin

    Take the code above, and put it into a function in crystal. Then pass your hex value to it, and it will result in the decimal value.

  • WuKa

    try this
    Function ConvertChar (Value as Number)
    Dim Result as String
    select case Value
    case 0
    Result = “0″
    case 1
    Result = “1″
    case 2
    Result = “2″
    case 3
    Result = “3″
    case 4
    Result = “4″
    case 5
    Result = “5″
    case 6
    Result = “6″
    case 7
    Result = “7″
    case 8
    Result = “8″
    case 9
    Result = “9″
    case 10
    Result = “A”
    case 11
    Result = “B”
    case 12
    Result = “C”
    case 13
    Result = “D”
    case 14
    Result = “E”
    case 15
    Result = “F”
    End Select
    ConvertChar = Result
    End FunctionFunction ConvertChar (Value as Number)
    Dim Result as String
    select case Value
    case 0
    Result = “0″
    case 1
    Result = “1″
    case 2
    Result = “2″
    case 3
    Result = “3″
    case 4
    Result = “4″
    case 5
    Result = “5″
    case 6
    Result = “6″
    case 7
    Result = “7″
    case 8
    Result = “8″
    case 9
    Result = “9″
    case 10
    Result = “A”
    case 11
    Result = “B”
    case 12
    Result = “C”
    case 13
    Result = “D”
    case 14
    Result = “E”
    case 15
    Result = “F”
    End Select
    ConvertChar = Result
    End Function

    Function StringToHex (ValString as String)
    Dim Result as String
    Dim Value as Number
    Value = ToNumber(ValString)
    While (Value / 16 ) > 0
    Result = Result + ConvertChar(Value mod 16)
    Value = Value \ 16
    Wend
    StringToChar = StrReverse (Result)
    End Function

  • Hash

    Hi, Thanks a lot. I just used your code and it worked. And I also learned a bit about crystal reports as well.

    Thanks a Lot..

Leave a Reply

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...