How do I reverse tostring(table) in Lua

Viewed 122

In Lua when you use the method tostring(table) it returns something like this: table: 0xb5b1f0. So I was wondering if there is any way of reversing this and turning it back into the regular table.

1 Answers

This actually prints the pointer to the table data as a hex integer. You cannot use the numeric value of a pointer in Lua to access the data directly (in a standard way).

If you are really interested, you can serialize (convert a table to a string) (recursively), and then back into a table, but that is much less convenient than an 8 digit hex. If you are using any framework or library, there is a good chance that it comes with a built-in table serialization function. If not, then just look up "lua table serialization function" on any search engine. (I'll find a good function and write it here)

Something else you might want to know, is that you can do something like this with functions: string.dump will dump the Lua binary for the function in a Lua string, and can then later be converted to a function using loadstring().

Related