PDA

View Full Version : pack "d" in lua?



Triode
2010-01-24, 14:34
Anyone got a suggestion of how to create a lua string which is the floating point double resentation of a lua number?

In perl I can use pack "d", but there doesn't appear to be an equivalent for lua given our libraries. Ideally want to do this in pure lua rather than adding any C code.

bluegaspode
2010-01-24, 15:07
indifference_engine uses


string.format("%X", tonumber(args[i])) -- convert to hex.

to get the hex representation of a number. Maybe you can build upon it ?

Triode
2010-01-24, 15:17
Read the implementation of this - unfortunately doesn't support packing the
binary values (there's a patch on the net which extends it though - but this
breaks my avoiding C implementation requirement)
>
> string.format("%X", tonumber(args[i])) -- convert to hex.
>

bluegaspode
2010-01-24, 15:53
if you use
string.format('%e', number)
you get a string where sign, exponent and mantissa are written down.

split the string and convert each part to its binary representation.

Very ugly of course *G*.