mcbeen.com

Crystal Reports Hex Conversion

by 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;

:

10 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..

  • Alex

    Hi! I just used your code to convert string to hex and for the 2 different strings, I have same result in hex. Is this how it supposes to be?
    “42068”
    and
    “44068” returns “104.0”
    Do I do something wrong or this is how it must be?
    Thanks.
    Alex.

    • Danlor

      Its not clear to me what you are trying to do. The purpose of this little chunk if code is to give a simple way to get a decimal representation of a hexadecimal value. You supply it with a 2-byte value like “1a” and it will give you a decimal value.

      One thing to note, is that the script expects to be fed a value with a 0x header. Its possible that your leading two digits are being cut off. For speed reasons I avoided any complex filtering logic.

  • Angel

    Thanks a lot!
    It’s perfect and useful

  • Shawna

    Hi,
    I am getting an error message, “The string is non-numeric” higlighting the “ToNumber(ValString)” on the function StringToHex. I double-checked and the field is a string field. Any suggestions?

  • Sally

    Hello,

    You wouldn’t happen to have a bitwise chunk of code for Crystal, would you?

    I have a bit field representing different meanings. For example, the stored decimal value could be 321, meaning history, charged, approved

    Hex Decimal
    100 256 History
    40 64 Charged
    1 1 Approved

    • Danlor

      No, I don’t… but I can see how that could be very useful. I’ll look into it, but can’t make any promises. As you can tell I don’t update things here very often, but they do get updated!

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...