开发者

store string of bytes in table in lua

i need to store a string of bytes in a开发者_Python百科 table in lua, how I can do it thanks Jp


Is that what you mean?

s="some string"
t={s:byte(1,#s)}


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜