Page 1 of 1

How to covert int to hex type value and save it in a string?

Posted: Fri Dec 16, 2011 5:23 pm
by bobwalker
How to covert int to hex type value and save it in a string?
It is like that Routeros doest have the script function (but have toarray,tobool,totime etc.).
Doesnt truely RouterOS have the script function coverting int to hex ??
I need it in a layer7filter-script.
Thanks!

Re: How to covert int to hex type value and save it in a str

Posted: Sun Dec 18, 2011 11:09 am
by TealFrog
I don't recall where I found this, but it should help...
# $dec has decimal value to convert
:local dec 3735928559
# $hexdigit set this to number of hex digits (max) you want
:local hexdigit 8
# $hex is string containing hex value
:local hex ""
:for i from=0 to=(4*($hexdigit-1)) step=4 do={
   :set hex ([:pick "0123456789ABCDEF" (($dec>>$i)&0xf) ((($dec>>$i)&0xf)+1)].$hex)
}
:log info ("0x".$hex)