Controller
Controller

Reputation: 529

How to print numbers read from a binary file in lua?

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?

Upvotes: 1

Views: 1088

Answers (1)

lhf
lhf

Reputation: 72312

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.

Upvotes: 3

Related Questions