I felt nominated, here’s a little gift…
based on:
http://forum.mikrotik.com/t/rextended-fragments-of-snippets/151033/18
Obtain a bit value from a byte (expected input from 0x00 to 0xFF, and the wanted bits. If the bits number is not specified, convert all byte to bit.
0x can be omitted
:global getMyBitFromHex do={
:local conv $1
:if (!($conv~"(^0x|^)[0-9a-fA-F]{2}\$")) do={:return "00000000"}
:if ([:typeof [:find $conv "0x" -1]] = "nil") do={:set conv "0x$conv"}
:local number [:tonum $conv]
:local wanted [:tostr "$2"]
:if ($wanted = "") do={:set wanted "87654321"}
:local values [:toarray "x,0,0,0,0,0,0,0,0"]
:local power 1
:local output ""
:for position from=1 to=8 step=1 do={
:if ($number & $power) do={:set ($values->$position) 1}
:set power ($power * 2)
:if ([:len [:find $wanted $position]] > 0) do={
:set output "$($values->$position)$output"
}
}
:return $output
}
As wanted bit(s) can be specified one or more position.
Position is from 8 to 1.
> :put [$getMyBitFromHex “0xA8”]
10101000
> :put [$getMyBitFromHex “0xA8” 8]
1
> :put [$getMyBitFromHex “0xA8” 765]
010 <<== is 2
> :put [$getMyBitFromHex “0xA8” 4321]
1000 <<== is 8
no matter the specified bits order, or if is specified a wrong position or same time the same position.