+ to_ieee754
parent
b04b46e899
commit
43653c1843
|
@ -25,3 +25,7 @@ function hook_chunks() {
|
|||
pkt_chunk 00000001 00000000
|
||||
pkt_chunk 00000001 00000001
|
||||
}
|
||||
|
||||
function hook_block() {
|
||||
|
||||
}
|
||||
|
|
41
src/int.sh
41
src/int.sh
|
@ -99,6 +99,7 @@ function hex2bin() {
|
|||
}
|
||||
|
||||
# from_ieee754(hexstring)
|
||||
# this works only on ints
|
||||
function from_ieee754() {
|
||||
local sign
|
||||
local exponent
|
||||
|
@ -130,6 +131,46 @@ function from_ieee754() {
|
|||
fi
|
||||
}
|
||||
|
||||
# to_ieee754(number)
|
||||
# this works only on ints
|
||||
function to_ieee754() {
|
||||
local n
|
||||
local m
|
||||
local i
|
||||
local sign
|
||||
local mantissa
|
||||
|
||||
n=$1
|
||||
m=2
|
||||
i=0
|
||||
sign=0
|
||||
|
||||
if [[ $n -lt 0 ]]; then
|
||||
n=$((n-2*n))
|
||||
sign=1
|
||||
fi
|
||||
|
||||
while true; do
|
||||
if [[ $n -lt $((m*2)) ]]; then
|
||||
break
|
||||
fi
|
||||
i=$((i+1))
|
||||
m=$((m*2))
|
||||
done
|
||||
|
||||
mantissa=$(printf "1%010d" $(hex2bin $(printf "%x" $i)))
|
||||
|
||||
# leading zeroes kept screwing me over, I'm hardcoding 1 and -1
|
||||
if [[ $n == -1 ]]; then
|
||||
printf "bff0000000000000"
|
||||
elif [[ $n == 1 ]]; then
|
||||
printf "3ff0000000000000"
|
||||
else
|
||||
res=$sign$mantissa$(printf "%0$((i+1))d" $(hex2bin $(printf "%x" $((n-m))) | sed -E 's/^0*//'))
|
||||
printf "%x" $((2#$res$(repeat $((64-$(echo -n $res | wc -c))) 0)))
|
||||
fi
|
||||
}
|
||||
|
||||
# to_short(number)
|
||||
function to_short() {
|
||||
if [[ $1 -lt 0 && $1 -gt -32769 ]]; then
|
||||
|
|
Loading…
Reference in New Issue