1

I have a binary file and I want to read its contents with lua. I know that it contains float numbers represented as 4 bytes with no delimeters between them. So I open the file and do t=file:read(4). Now I want to print the non-binary representation of the number, but if I do print(t), I only get sth like x98xC1x86. What should I do?

0

1 Answer 1

3

If you're running Lua 5.3, try this code:

t=file:read(4)
t=string.unpack(t,"f")
print(t)

The library function string.unpack converts binary data to Lua types.

Sign up to request clarification or add additional context in comments.

3 Comments

I am using this in torch, which comes with lua 5.2. So, is there any alternative?
You can use Robertos struct library to get the same functionality in older versions of Lua.
Also my own lpack.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.