Thanks for sharing,
There are very few errors, and the writing style is acceptable.
I like that you are inspired from my scripts…
http://forum.mikrotik.com/t/rextended-fragments-of-snippets/151033/1
just at the start…
:local strMessage [:tostr “$1”]
is like
:local strMessage [:tostr [:tostr $1 ]]
so, why not simply
:local strMessage [:tostr $1]
?
missing all 0x80..0xFF characters…
chr2int is simpler for convert character to decimal
http://forum.mikrotik.com/t/newb-in-scripting-getting-byte-value-from-lease-options-string/162848/1
dec2bin is faster and more easy for convert number to binary
http://forum.mikrotik.com/t/rextended-fragments-of-snippets/151033/1
using both as base, this create one array on memory for all 255 possible values already calculated, is faster than do everytime all the opreation for each single characters:
:global charsString “”
:for x from=0 to=15 step=1 do={ :for y from=0 to=15 step=1 do={
:local tmpHex “$[:pick “0123456789ABCDEF” $x ($x+1)]$[:pick “0123456789ABCDEF” $y ($y+1)]”
:set $charsString “$charsString$[[:parse “("\$tmpHex")”]]”
} }
:global dec2bin do={
:local number [:tonum $1]
:local ret “”
:local rshift 7
:if ($number > 0xFF) do={:set rshift 15}
:if ($number > 0xFFFF) do={:set rshift 31}
:if ($number > 0xFFFFFFFF) do={:set rshift 63}
:for i from=0 to=$rshift step=1 do={
:set ret “$(($number >> $i) & 1)$ret”
}
:return $ret
}
:global arrCharBin [:toarray “”]
:for x from=0 to=255 step=1 do={
:set ($arrCharBin->[:pick $charsString $x ($x + 1)]) [$dec2bin $x]
}
for convert a character, simply get the keyed value on the array with same name as the character…
[rex@home] > :put ($arrCharBin->“v”)
01110110
[rex@home] > :put ($arrCharBin->“i”)
01101001
[rex@home] > :put ($arrCharBin->“v”)
01110110
[rex@home] > :put ($arrCharBin->“a”)
01100001
[rex@home] > :put ($arrCharBin->“l”)
01101100
[rex@home] > :put ($arrCharBin->“a”)
01100001
[rex@home] > :put ($arrCharBin->“f”)
01100110
[rex@home] > :put ($arrCharBin->“c”)
01100011
[rex@home] > :put ($arrCharBin->“a”)
01100001
[rex@home] >