Hi again,
After some more help from Stackoverflow I now have the two following algorithms:
function float2nibbles(value)
local nibbles = MemoryBlock(4, true)
local n = math.floor(math.abs(value) * 256 + 0.13)
n = value < 0 and 0x10000 - n or n
for pos = 0, 3 do
nibbles:setByte(pos, n % 16)
n = math.floor(n / 16)
end
return nibbles
end
function nibbles2float(memBlock, offset)
local bi = BigInteger(0)
bi:setBitRangeAsInt(0, 4, memBlock:getByte(offset))
bi:setBitRangeAsInt(4, 4, memBlock:getByte(offset + 1))
bi:setBitRangeAsInt(8, 4, memBlock:getByte(offset + 2))
bi:setBitRangeAsInt(12, 4, memBlock:getByte(offset + 3))
local n = 0
for i = 0, 15 do
local factor = math.pow(2, i - 8)
n = n + bi:getBitRangeAsInt(i, 1) * factor
end
return memBlock:getByte(offset + 3) >= 0x8 and n - 256 or n
end
For more information you can read this thread on SO: http://stackoverflow.com/questions/38912827/convert-floating-point-values-to-hexadecimal
-
This reply was modified 7 years, 8 months ago by pascalc.
-
This reply was modified 7 years, 8 months ago by pascalc.