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?
                    
                        Add a comment
                    
                 | 
            
                
            
        
         
    1 Answer
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.
3 Comments
Controller
 I am using this in torch, which comes with lua 5.2. So, is there any alternative?
  David Crayford
 You can use Robertos struct library to get the same functionality in older versions of Lua.