lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Tim Channon wrote:
Clark Snowdall wrote:
Hello all,

I'm a newbie here so be gentle ...

I need to convert back and forth between a string of bytes (4 bytes,
32-bits), and IEEE-754 floating point.  Basically I need to read a
byte stream from a network socket and convert those bytes to a
number.

There is the tiny Struct library (8.4k of c source) by the authors of
Lua which might help but gives a different answer on this computer, so
maybe it is using a different fp standard.

http://www.inf.puc-rio.br/~roberto/struct/

require("struct")
x = string.char(0x42, 0x96, 0x00, 0x00)
ans=struct.unpack(">!32f",x) -- big endian 32bit float
print(ans)
75

75 is correct, the function I provided assumes the data is in little endian form.

--
Cheers,
Kein-Hong Man (esq.)
Kuala Lumpur, Malaysia