lua-users home
lua-l archive

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


> 64-bit IEEE floating point:
> <<Sign:1,Exponent:11,Mantissa:52>>
> 
> IP datagram:
> <<?IP_VERSION:4, HLen:4, SrvcType:8, TotLen:16, 
>       ID:16, Flgs:3, FragOff:13,
>       TTL:8, Proto:8, HdrChkSum:16,
>       SrcIP:32, DestIP:32, RestDgram/binary>>

Given these strings it easy to parse them into a Lua table containing the
names and starting and end points of the bitfields described there. That
table can then be used to set index and newindex metamethods for get and
put the bitfields using Lua syntax, or perhaps pack and unpack functions:

template = "<<Sign:1,Exponent:11,Mantissa:52>>"
name = "64-bit IEEE floating point"
b = bitfelds.parse(name,template)
s = io.read(file,b.size)
t = b:unpack(s)
print(t.Sign,t.Exponent,t.Mantissa)
t.Exponent=0
s = b:pack(t)