search tag # rextended int2hex num2hex integer number hexadecimal convert function
Function to convert integer to hex (only positive numbers)
:global num2hex do={
:local number [:tonum $1]
:local hexadec "0"
:local remainder 0
:local hexChars "0123456789ABCDEF"
:if ($number > 0) do={:set hexadec ""}
:while ( $number > 0 ) do={
:set remainder ($number % 16)
:set number (($number-$remainder) / 16)
:set hexadec ([:pick $hexChars $remainder].$hexadec)
}
:if ([:len $hexadec] = 1) do={:set hexadec "0$hexadec"}
:return "0x$hexadec"
}
:put [$num2hex 7366]
For simple 0-255 to hex:
:global numbyte2hex do={
:local input [:tonum $1]
:local hexchars "0123456789ABCDEF"
:local convert [:pick $hexchars (($input >> 4) & 0xF)]
:set convert ($convert.[:pick $hexchars ($input & 0xF)])
:return $convert
}
the script here
https://forum.mikrotik.com/viewtopic.php?f=2&t=57665&p=868898#p295501
must be simply updated and converted on a function and work better than mine to convert for example integer to signed Single Word Hexadecimal value,
with “right” byte shift and “and 0xf” can convert easily -123 on 2 signed byte to 0xFF85
Probably a reverse-function can be writed based on that method.