How to work with binary data?
Binary data can be handled as Lua strings, since strings are not NUL-terminated.
> s = 'one\0two\0three'
> = s one
> = #s
13
> for k in s:gfind '%Z+' do print(k) end one two three
Note that although the whole string could not be printed out, the length operator indicates that all bytes are present! string.gfind
iterates over a string pattern; '%z' means the NUL byte, and '%Z' means 'anything except the NUL byte'.
Bear also in mind that '\ddd' escapes in Lua strings are decimal, not octal as they are in C. There is also no hexadecimal escape like '\xDD' although it is being considered for Lua 5.2
If I have a string containing the two bytes of a 16-bit integer, then s:byte(2)+256*s:byte(1)
would do the job, if the byte order was little-endian.
To actually unpack non-trivial values from binary strings, a little help is needed. Roberto Ierusalimschy's struct library is useful here, since it knows about endianness and alignment issues:
For example, to pack a 32-bit float and a 16-bit bit integer into a string:
> s = struct.pack('hf',42,42)
> = #s
6
> = struct.unpack ('hf',s)
42 42 7
The last value returned is the point where we stopped reading, which in this case is beyond the end of the string. You can use this index to go through a array of structures.