search tag # rextended base16dec base16 to string or data decoding
Function to convert Base16 to a string (or a Byte stream)
Note: the function accept mixed A-F a-f case.
Option “ignoreodd” ignore if the decoded string have odd value (must be have only character pairs).
pratically is a revised function old hexstr2chrstr:
http://forum.mikrotik.com/t/decode-ussd-on-wap-lte-kit/115644/4
:global base16dec do={
:local input [:tostr "$1"]
:local options [:tostr "$2"]
:local hex2chr do={:return [[:parse "(\"\\$1\")"]]}
:local lowerarray {"a"="A";"b"="B";"c"="C";"d"="D";"e"="E";"f"="F"}
:if (!($input~"^[0-9A-Fa-f]*\$")) do={
:error "invalid characters: only 0-9, A-F and a-f are valid Base16 values"
}
:if (!($options~"ignoreodd")) do={
:if (([:len $input] % 2) != 0) do={:error "Invalid length, is odd."}
}
:local position 0
:local output "" ; :local work "" ; :local chk1 "" ; :local chk2 ""
:while ($position < [:len $input]) do={
:set chk1 [:pick $input $position ($position + 1)]
:set chk2 [:pick $input ($position + 1) ($position + 2)]
:if ($chk1~"[a-f]") do={:set chk1 ($lowerarray->$chk1)}
:if ($chk2~"[a-f]") do={:set chk2 ($lowerarray->$chk2)}
:set work "$chk1$chk2"
:if ([:len $work] = 2) do={
:set work [$hex2chr $work]
} else={
:set work ""
}
:set output "$output$work"
:set position ($position + 2)
}
:return $output
}
> :put [$base16dec “48692066726F6D204D696B726F54696B”]
Hi from MikroTik
> :put [$base16dec “48692066726F6D204D696B726F54696” “ignoreodd”]
Hi from MikroTi