i need to store a string of bytes in a table in lua, how I can do it thanks Jp
2 Answers
Is that what you mean?
s="some string"
t={s:byte(1,#s)}
1 Comment
jprbest
no dont want to convert the string i just want to have the bytes like for example "C0020101" to be in t={c0,02,01,01} this what i meant
A Lua string is exactly what you wrote - a string of bytes. Lua is different from C-like languages in that it is 8-bit clean, meaning that you can even store embedded zero '\0' inside strings - the length of the string is held separately and is not based on where '\0' is.
You did not write where you want those bytes from (what is the source), so let's assume you are reading from a file. In the following example, f is a file handle obtained by calling io.open(filename), and t is a table (t = {}).
local str = f:read(100) -- will read up to 100 bytes from file handle f
t[#t + 1] = str -- will append the string to the end of table t
table.insert(t, str) -- alternative way of achieving the same