How can I save/read data in binary format?

So basically, I want to save game data encoded in hex directly into a binary file, but the resulting file is still in text format (and can be opened plainly in a text editor). How do I make sure the resulting file is a binary file?

In essence this is my code:

-- This function takes a normal text string and coverts to hexadecimal  
function tohex(str)  
 return (str:gsub('.', function (c)  
 return string.format("%02X", string.byte(c))  
end))  
  
file = io.open( filePath, "wb" )  
file:write(tohex("whatever string here"))  
io.close(file)  

As mentioned above, the resulting file is plainly viewable with a text editor and isn’t hex encoded, the file size is also twice of what it should be (as 2 hex chars are used to represent one string char). Am I doing something wrong, or is Corona/lua unable to write files directly into binary?

I have taken a look at http://www.lua.org/pil/21.2.2.html but it does not tell you how to ensure that the file saved is a binary (since lua outputs in strings).

Is there a way to create binary files in Corona using lua? [import]uid: 108204 topic_id: 30314 reply_id: 330314[/import]

I would like to know this too. [import]uid: 74723 topic_id: 30314 reply_id: 121651[/import]

I would like to know this too. [import]uid: 74723 topic_id: 30314 reply_id: 121651[/import]