I ran across string.dump(). I’m trying to find a way to use string.dump() to output the binary to a tab that i can then load back with loadstring(). However I can not get this to work. If I pass the string directly to loadstring it works. If I load from a tab I get errors. I have no idea what use this would be but I find it interesting. Simply copy this code into setup of a new project. a function will be run that prints “I ran a Function”.
lol I knew it would turn some heads. It took me awhile to figure out how to actually read the byte string. If you simply print the output of string.dump() you get LuaQ. If I could only figure out a way to save the string as a comment. putting – in front of it seems to mess something up. I remove the – with string.sub but then I get errors.
You’ll find some examples around about storing into an image (one from me).
Just remember to force a=255 otherwise it doesnt work due to apple storage format (premultiplied)
I’ve got it sttored into an image just fine but I can’t read it back out. I get an identical string out of it but there is some character that loadstring doesnt like.
Ok I’m running into an issue with loading back the data. Here is a simple example and it appears that lua is somehow treating the strings different. If you guys could try it out and see what I’m talking about.
--# Main
-- test
-- Use this function to perform your initial setup
function setup()
print("Hello World!")
binary = string.dump(t)
formatted_binary = ''
for i = 1, string.len(binary) do
dec, _ = ("\\\\%3d"):format(binary:sub(i, i):byte()):gsub(' ', '0')
formatted_binary = formatted_binary .. dec
end
--I used this to dump the code to test tab. I then cut it, named variable teststr = '*pasted code*'
--saveProjectTab("test",formatted_binary)
--I saved formated_binary to tab test. Then assigned the string to variable teststr
assert(loadstring(teststr))() -- This runs the variable teststr which works
--This is passing the same string that was saved to the tab
assert(loadstring(formatted_binary),"This is broke")()
--It seems that lua is doing something to the string differently
end
t = function()
print("Look I ran a function!")
end
--# test
teststr = '\\027\\076\\117\\097\\081\\000\\001\\004\\004\\004\\004\\000\\140\\003\\000\\000\\045\\045\\032\\116\\101\\115\\116\\010\\010\\045\\045\\032\\085\\115\\101\\032\\116\\104\\105\\115\\032\\102\\117\\110\\099\\116\\105\\111\\110\\032\\116\\111\\032\\112\\101\\114\\102\\111\\114\\109\\032\\121\\111\\117\\114\\032\\105\\110\\105\\116\\105\\097\\108\\032\\115\\101\\116\\117\\112\\010\\102\\117\\110\\099\\116\\105\\111\\110\\032\\115\\101\\116\\117\\112\\040\\041\\010\\032\\032\\032\\032\\010\\032\\032\\032\\032\\112\\114\\105\\110\\116\\040\\034\\072\\101\\108\\108\\111\\032\\087\\111\\114\\108\\100\\033\\034\\041\\010\\032\\032\\032\\098\\105\\110\\097\\114\\121\\032\\061\\032\\115\\116\\114\\105\\110\\103\\046\\100\\117\\109\\112\\040\\116\\041\\010\\032\\032\\032\\032\\102\\111\\114\\109\\097\\116\\116\\101\\100\\095\\098\\105\\110\\097\\114\\121\\032\\061\\032\\039\\039\\010\\032\\032\\032\\032\\102\\111\\114\\032\\105\\032\\061\\032\\049\\044\\032\\115\\116\\114\\105\\110\\103\\046\\108\\101\\110\\040\\098\\105\\110\\097\\114\\121\\041\\032\\100\\111\\010\\032\\032\\032\\032\\032\\032\\032\\032\\100\\101\\099\\044\\032\\095\\032\\061\\032\\040\\034\\092\\092\\037\\051\\100\\034\\041\\058\\102\\111\\114\\109\\097\\116\\040\\098\\105\\110\\097\\114\\121\\058\\115\\117\\098\\040\\105\\044\\032\\105\\041\\058\\098\\121\\116\\101\\040\\041\\041\\058\\103\\115\\117\\098\\040\\039\\032\\039\\044\\032\\039\\048\\039\\041\\010\\032\\032\\032\\032\\032\\032\\032\\032\\102\\111\\114\\109\\097\\116\\116\\101\\100\\095\\098\\105\\110\\097\\114\\121\\032\\061\\032\\102\\111\\114\\109\\097\\116\\116\\101\\100\\095\\098\\105\\110\\097\\114\\121\\032\\046\\046\\032\\100\\101\\099\\010\\032\\032\\032\\032\\101\\110\\100\\010\\032\\032\\032\\045\\045\\032\\102\\111\\114\\109\\097\\116\\116\\101\\100\\095\\098\\105\\110\\097\\114\\121\\032\\061\\032\\102\\111\\114\\109\\097\\116\\116\\101\\100\\095\\098\\105\\110\\097\\114\\121\\032\\046\\046\\032\\034\\093\\093\\034\\010\\115\\097\\118\\101\\080\\114\\111\\106\\101\\099\\116\\084\\097\\098\\040\\034\\116\\101\\115\\116\\034\\044\\102\\111\\114\\109\\097\\116\\116\\101\\100\\095\\098\\105\\110\\097\\114\\121\\041\\010\\045\\045\\116\\109\\112\\032\\061\\032\\066\\097\\115\\101\\054\\052\\046\\100\\101\\099\\111\\100\\101\\040\\098\\105\\110\\097\\114\\121\\041\\010\\045\\045\\112\\114\\105\\110\\116\\040\\116\\109\\112\\041\\010\\045\\045\\116\\109\\112\\032\\061\\032\\066\\097\\115\\101\\054\\052\\046\\100\\101\\099\\111\\100\\101\\040\\116\\109\\112\\041\\010\\010\\045\\045\\108\\111\\099\\097\\108\\032\\116\\109\\112\\032\\061\\032\\114\\101\\097\\100\\080\\114\\111\\106\\101\\099\\116\\084\\097\\098\\040\\034\\116\\101\\115\\116\\034\\041\\010\\045\\045\\116\\109\\112\\032\\061\\032\\115\\116\\114\\105\\110\\103\\046\\115\\117\\098\\040\\116\\109\\112\\044\\052\\044\\035\\116\\109\\112\\041\\010\\045\\045\\112\\114\\105\\110\\116\\040\\116\\109\\112\\091\\050\\093\\041\\010\\045\\045\\112\\114\\105\\110\\116\\040\\116\\109\\112\\041\\010\\097\\115\\115\\101\\114\\116\\040\\108\\111\\097\\100\\115\\116\\114\\105\\110\\103\\040\\116\\101\\115\\116\\115\\116\\114\\041\\041\\040\\041\\010\\010\\010\\101\\110\\100\\010\\010\\045\\045\\032\\084\\104\\105\\115\\032\\102\\117\\110\\099\\116\\105\\111\\110\\032\\103\\101\\116\\115\\032\\099\\097\\108\\108\\101\\100\\032\\111\\110\\099\\101\\032\\101\\118\\101\\114\\121\\032\\102\\114\\097\\109\\101\\010\\102\\117\\110\\099\\116\\105\\111\\110\\032\\100\\114\\097\\119\\040\\041\\010\\032\\032\\032\\032\\045\\045\\032\\084\\104\\105\\115\\032\\115\\101\\116\\115\\032\\097\\032\\100\\097\\114\\107\\032\\098\\097\\099\\107\\103\\114\\111\\117\\110\\100\\032\\099\\111\\108\\111\\114\\032\\010\\032\\032\\032\\032\\098\\097\\099\\107\\103\\114\\111\\117\\110\\100\\040\\052\\048\\044\\032\\052\\048\\044\\032\\053\\048\\041\\010\\010\\032\\032\\032\\032\\045\\045\\032\\084\\104\\105\\115\\032\\115\\101\\116\\115\\032\\116\\104\\101\\032\\108\\105\\110\\101\\032\\116\\104\\105\\099\\107\\110\\101\\115\\115\\010\\032\\032\\032\\032\\115\\116\\114\\111\\107\\101\\087\\105\\100\\116\\104\\040\\053\\041\\010\\010\\032\\032\\032\\032\\045\\045\\032\\068\\111\\032\\121\\111\\117\\114\\032\\100\\114\\097\\119\\105\\110\\103\\032\\104\\101\\114\\101\\010\\032\\032\\032\\032\\010\\101\\110\\100\\010\\010\\116\\032\\061\\032\\102\\117\\110\\099\\116\\105\\111\\110\\040\\041\\010\\032\\032\\032\\032\\112\\114\\105\\110\\116\\040\\034\\076\\111\\111\\107\\032\\073\\032\\114\\097\\110\\032\\097\\032\\102\\117\\110\\099\\116\\105\\111\\110\\033\\034\\041\\010\\101\\110\\100\\010\\000\\040\\000\\000\\000\\042\\000\\000\\000\\000\\000\\000\\002\\004\\000\\000\\000\\005\\000\\000\\000\\065\\064\\000\\000\\028\\064\\000\\001\\030\\000\\128\\000\\002\\000\\000\\000\\004\\006\\000\\000\\000\\112\\114\\105\\110\\116\\000\\004\\023\\000\\000\\000\\076\\111\\111\\107\\032\\073\\032\\114\\097\\110\\032\\097\\032\\102\\117\\110\\099\\116\\105\\111\\110\\033\\000\\000\\000\\000\\000\\004\\000\\000\\000\\041\\000\\000\\000\\041\\000\\000\\000\\041\\000\\000\\000\\042\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000'
@Briarfox I think I have your problem figured out. I was able to create the function string ( all the numbers ) in a tab. Manually copy that tab and paste it in the tab of a new program. Read that tab in the new program and execute the original function. Is that what you were trying to do. If so, I’ll post my code when I get it organized better.
@Ignatz great observation! Now I know where to look. I just tried to create a new var str = formatted_binary but it didn’t seem to help. Any ideas? Your right about how it’s saving the string but I can’t think of a way around it. I’ve tried to save and read in projectdata. No luck.
@Briarfox Here’s the code I was playing with. I’m still not sure what’s not working with yours. This creates a tab called ggg containing a string g. The first time this runs, there is an error because the string g doesn’t exist. Comment out the statement createStr() and execute it again, it will print ‘Look I ran a function’. If you copy the tab and paste it at the end of the program and delete the tab ggg, running the program again prints ‘Look I ran a function’. If I copy the tab and paste it into another program, it still prints ‘Look I ran a function’.
function setup()
createStr()
executeStrG()
end
function createStr()
b=string.dump(t)
f="g='"
for i = 1, string.len(b) do
f=f..string.format("\\\\%03d",string.byte(string.sub(b,i,i)))
end
f=f.."'"
saveProjectTab("ggg",f)
end
function executeStrG()
assert(loadstring(g))()
end
t = function()
print("Look I ran a function!")
end
@dave1707 But If I try to load f directly I get an error.
function setup()
createStr()
executeStrG()
end
function createStr()
b=string.dump(t)
f=""--"g='"
for i = 1, string.len(b) do
f=f..string.format("\\\\%03d",string.byte(string.sub(b,i,i)))
end
-- f=f.."'"
--saveProjectTab("ggg",f)
end
function executeStrG()
assert(loadstring(f))()
end
t = function()
print("Look I ran a function!")
end
@Briarfox I re-read what you were saying in your original post and realized that f doesn’t work. I’m in the process of see what is different between f and b or g.