Logic Machine Forum
How to insert the data in table into the string? - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: How to insert the data in table into the string? (/showthread.php?tid=2747)



How to insert the data in table into the string? - 38348259 - 25.07.2020

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


RE: How to insert the data in table into the string? - admin - 27.07.2020

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)