Forum Discussion

Magnum_IP's avatar
Magnum_IP
Icon for Nimbostratus rankNimbostratus
Feb 07, 2013

Hex to Dec in an iRule

Can anybody help?

 

I need to convert a a hex number to decimal in an irule. The hex number can be in one of 2 formats eg.

 

0x01 0xf4

 

01f4

 

I would like to convert this to its decimal equivalent, in the case of this example 500.

 

Regards,

 

fergu5

 

3 Replies

  • iRules are TCL at heart, so usually an operation that that is not met with the iRule functionality described on DevCentral can still be met using native TCL tools. In this case I think this is probably what you are looking for: http://wiki.tcl.tk/3242. The examples on that page may not be the most efficient, but it looks like they'll do what you are asking about.

     

     

     

  • This isn't too hard.

    For the first format, you'll want to bundle both hex values into a single one (normalize them) and process it like you would the second format. For the second format, it's very simple:

    set hex "0x01f4"
    set decimal [expr $hex] 

    or

     set hex "0x01f4"
    scan $hex %x decimal

    That would produce "500" as you want in $decimal.

  • Posted By Joel Moses on 02/07/2013 04:18 PM

    This isn't too hard.

    For the first format, you'll want to bundle both hex values into a single one (normalize them) and process it like you would the second format. For the second format, it's very simple:

    set hex "0x01f4"

    set decimal [expr $hex]

    or

     set hex "0x01f4"

    scan $hex %x decimal

    That would produce "500" as you want in $decimal.

    Joel - Thanks very much. Using expr method.

    tcl to the rescue!

    fergu5