Forum Discussion
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
- Jessed12345
Employee
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.
- Joel_Moses
Nimbostratus
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.
- Magnum_IP
Nimbostratus
Posted By Joel Moses on 02/07/2013 04:18 PMThis 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
Recent Discussions
Related Content
* Getting Started on DevCentral
* Community Guidelines
* Community Terms of Use / EULA
* Community Ranking Explained
* Community Resources
* Contact the DevCentral Team
* Update MFA on account.f5.com