This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

How to insert the data in table into the string?
#1
Dear All:

I want to insert the data of table into a string. 
e.g. 
table = {0x01,0x02,0x03,0x04}
string.char(0x0A,0x0B,0x0C,0x0D)
I want to insert all the data in the table between 0x0B and 0x0C to become string.char(0x0A,0x0B,0x01,0x02,0x03,0x04,0x0C,0x0D). How to realize that?

Thanks a lot
Reply
#2
You can use table.insert to add table elements at any position you need. Don't use table variable name because this will override table functions.
Code:
t = {0x01,0x02,0x03,0x04}

table.insert(t, 1, 0x0A) -- add as first table element
table.insert(t, 2, 0x0B) -- add as second table element

table.insert(t, 0x0C) -- add as last table element
table.insert(t, 0x0D) -- add as last table element

-- at this point table is { 0x0A, 0x0B, 0x01, 0x02, 0x03, 0x04, 0x0C, 0x0D }
str = string.char(unpack(t)) -- convert table to a binary string
loghex(str)
Reply


Forum Jump: