Hexy: Add small binary files inside your code

Post Reply
User avatar
Dav
Posts: 279
Joined: Tue Dec 30, 2014 5:12 pm
My devices: iPad Mini, iPod Touch.
Location: North Carolina, USA
Contact:

Hexy: Add small binary files inside your code

Post by Dav »

I was trying out the new commands in smart Basic v4.8 today (HEX/DEC/GZIP/GUNZIP) and made this little tool to allow me to put small files directly in source code (I still don't use dropbox :? ). This is more for fun and curiosity than for practical use. The new sB commands work very well I think.

- Dav


Hexy program below will hexify a file and output source code that when run will recreate that file in the current directory. You can use the file and delete it afterwards. I will also post an example of what Hexy can do.

Hexy v1.0 program

Code: Select all

'Hexy.txt v1.0 - A file hexifier
'Encodes small binary file to smart basic code
'that recreates the binary file when executed.
'Outputs series of .txt files for larger file.
'(ie: out1.txt,out2.txt, out3.txt, etc...)
'Coded by Dav, JUNE/2015


f$="davface.zip" '<<< name of file to hexify

if file_exists(f$)=0 then
   print f$&" not found." ! end
end if


option base 1
option screenlock off

num=1 ! of$="out"& str$(num)&".txt"
bc=0 ! lc=1

print "Creating "&of$&" from "&f$&"..."

r$="hx$="&chr$(34)&chr$(34)&" '"&f$
r$=r$&" - created by Hexy v1.0"
file of$ writeline r$

for t= 1 to file_size(f$)
  file f$ read a
  d$=hex$(a)
  if lc = 1 then 
     r$="hx$=hx$&"&chr$(34)
     file of$ writeline r$;
  end if
  if len(d$)=1 then
     file of$ write asc("0")
     file of$ write asc(d$)
  else
     file of$ write asc(mid$(d$,1,1))
     file of$ write asc(mid$(d$,2,1))
  end if
  bc=bc+1
  lc=lc+1
  if lc =20 then 
      file of$ write asc(chr$(34))
      file of$ write 10
      lc =1
  end if
  if bc > 30000 then
     bc=0 ! lc=1
     num=num+1
     of$="out"& str$(num)&".txt"
     print "Creating "&of$&"..."
  end if
next t

file of$ write asc(chr$(34))
file of$ write 10

file of$ writeline "option base 1"
r$="for t = 1 to len(hx$) step 2"
file of$ writeline r$
r$="  d$=chr$(dec(mid$(hx$,t,2)))"
file of$ writeline r$
r$="  file "&chr$(34)&f$&chr$(34)&" write asc(d$)"
file of$ writeline r$
r$="next t"
file of$ writeline r$

print "Done."

end
Here is and example of a file included in source code using Hexy program. This demo recreates a .zip file, unzips my forum avatar in .jpg format, loads the image and puts it on the screen randomly.

Code: Select all

'davface.txt
'Example of using Hexy to recreate a file.
'This code creates a .zip file, unzips it,
'then loads the .jpg file it contains.
'by Dav, JUNE/2015

'NOTE: This demo creates two files in the
'current directory: davface.zip & .jpg

hx$="" 'davface.zip - created by Hexy v1.0
hx$=hx$&"1F8B08000000000000039D957B3854FB1AC7D7"
hx$=hx$&"9831C39891C98C86E3B28C11E312C3109B69B6"
hx$=hx$&"CB4CA82143722BB91506C38489CA6547343BB5"
hx$=hx$&"518AF63E36B9A45C8B846AC756939EB41F1A77"
hx$=hx$&"21B925A7E37E2BE6CC74D9FB9FFDC739E7BB9E"
hx$=hx$&"67ADF7F77BBFEFFBFE3ECF7AD6B3447DA25140"
hx$=hx$&"C189E1C800206219400C00403408D801327038"
hx$=hx$&"022E2D83402064656590282C1A25278752DEAE"
hx$=hx$&"B80DABA6A2A1AEA6A2AA0A1277E98004032D55"
hx$=hx$&"55DDDD240363328542D1D0B1A45A9858EF32A5"
hx$=hx$&"98489A406465655172283C1A8D37D154D534F9"
hx$=hx$&"9F257A0C60640047C0090A2100521808140311"
hx$=hx$&"B5011A000091867C16F0551029284C1A8E9091"
hx$=hx$&"45CA890DF50A8014040A958241A5A5613071F6"
hx$=hx$&"8C380FC030D2DB35C9367045577F04211A6B92"
hx$=hx$&"925528A3655BDB826375CE114D03B83FC82295"
hx$=hx$&"76E09555B477EAE892F42866E6BB2D2CBFB3B3"
hx$=hx$&"A733F63A383AB9B91FF438E4E9E51D18147CEC"
hx$=hx$&"784868D88998D838DEC9F884B3A969E7D233CE"
hx$=hx$&"F3B3732E5FC9BD7A2D2FBFE846714969D9CDF2"
hx$=hx$&"5B77EED6D5DF6BB8DFD8D4FA7BDB93A78267ED"
hx$=hx$&"CFBB5E09BB7B7AFBFA07DE8CBD1D9F989C9A7E"
hx$=hx$&"3733BFB0B8B4BCB2BAB6BE21E1820050C837FD"
hx$=hx$&"2D1746CC2505834161080917448A27316060D2"
hx$=hx$&"9A64F8761B57847FB422C12445066B9B5558DB"
hx$=hx$&"22AB65CA9AC305703B914A44CA1BED7909DA67"
hx$=hx$&"B2FF0EEC87FF8BEC4FB0BFB806001414227E79"
hx$=hx$&"500C4003266E16F1975E82A5CD15215E1781C2"
hx$=hx$&"D781B61FA26E94575D6D1DC6EF3EFE91E335C8"
hx$=hx$&"297DBCBBE85994BC554E5749335A3F73AE3739"
hx$=hx$&"3649B1C570BC5A3BB394C6252F5AAE2E557158"
hx$=hx$&"B5426BB35B66E72ACA0255E99678449F08C84C"
hx$=hx$&"206AE0B75E0525DDA178290D98DED91127B82E"
hx$=hx$&"A0E305AE171FCEA415F0C3F7E808628B667293"
hx$=hx$&"021E9EDF6347523E661D88571F3AD5A3C77AD8"
hx$=hx$&"1FA1955994E3F6CAE893FE4D34CD9D4588D42C"
hx$=hx$&"A85DD9997BCF7932C253B788FF2B323C9BE85A"
hx$=hx$&"976DECEE9ACDDCEE90C578398B730FCAF1CE57"
hx$=hx$&"CF6253F6FACD63D867D485E9981ACB9C217E9E"
hx$=hx$&"155A3F3F51662E6858FB9758ABA590A5E98BD3"
hx$=hx$&"37FCB76A36F2A71F1CAB790FA76DFBDDD42E3C"
hx$=hx$&"D9FA9D325077F2E988E0135BEED0DA6AB18FCF"
hx$=hx$&"668E7D57D9C9EFF550D70F24A00066D4DC2786"
hx$=hx$&"9063549A2DA037C44F59159B3CEBC87A0C1CAD"
hx$=hx$&"824B1F2DBDDE4C890E0D8959EDD4EB4079DCAC"
hx$=hx$&"2DDE6F9E317FF135A9A93E35E761E5F2BD0833"
hx$=hx$&"0DCA8EC6858DAC575A739BD49992478D63E711"
hx$=hx$&"2AF21927364580F5A886EF3E37E6184DC987C3"
hx$=hx$&"9955D3FCE9D235D2B5C0C5DC062EF3B44107E3"
hx$=hx$&"B54B784F9DA1D5D301083F089297E8A1359979"
hx$=hx$&"FBF17284DEC7D6848295B8E28A001D85A124D9"
hx$=hx$&"959D557D9FA2851447229F09C7E9FC9BA1ADA5"
hx$=hx$&"B6F88C9AE9234CDC15F2C83C85F2D0CF217BE8"
hx$=hx$&"B9ADC1E8391941F229CC462F25F8F96C5EDD95"
hx$=hx$&"EE17DEA458A6CE81B992B59A6E850C9F4DFBA9"
hx$=hx$&"42F5D4AE434E3CE3B0A753671F5023C65C1452"
hx$=hx$&"9BF24EDFF6B54C2D78F4DA46F06BE5C88FF9F6"
hx$=hx$&"ABEA98FCE14E6449B9D7E93ABF07D805381541"
hx$=hx$&"4B8B8AEEE095F9D1CB43BBB0CD3FD658C85369"
hx$=hx$&"7F8880DB3D752E8A5E275F310753E46F6CDF5A"
hx$=hx$&"D0199D1AB893CF137619D94DD0AE652527CCF3"
hx$=hx$&"972FC1DEB98416F60F5798F7DF6779EFCBDB1F"
hx$=hx$&"6669A1DCD23E561FEB97F6741B6E60C433DE78"
hx$=hx$&"60BDA50A3C9D0CB7D176B18E7D7B21C9C867AE"
hx$=hx$&"2CDD60F6939574652BB15C59D3277AF6D48927"
hx$=hx$&"C7A56737277D0BF6644C5A9BAA3670F11F080D"
hx$=hx$&"976BB461C8289A2A4AF8AFA8A2E9C17676D993"
hx$=hx$&"82A6BB87B171D65190232E7BEA070C87738E9F"
hx$=hx$&"42915330CA9066752BC5DE4ABF8F4A05092321"
hx$=hx$&"0B015779F3CBCF4A67D871A9852555A75BE673"
hx$=hx$&"ECBF4B6CF4E7D6ACEF8B8DC8465E59BF92412B"
hx$=hx$&"C4094EFAD836CF23322AC20DB5E2E28399B3B5"
hx$=hx$&"57313B47E56A345A4D2CABCB1E69AFB1DEB635"
hx$=hx$&"05D5F6CB25DDC3A72AD48D8F461EF59C93B770"
hx$=hx$&"1D5CA8E0A0A7558C5AA970F6FB76FB8DCBB5A9"
hx$=hx$&"F1701BAEBCEDADF1D1A4B463BDA68DA1FD9EC3"
hx$=hx$&"6B3F5362E62AD2DD06EF1B7D48B19EC22DBEE0"
hx$=hx$&"792767DAD22DF7A37F6B56D3BFD1537F5D0428"
hx$=hx$&"8575DC0C93A7DFA7DD4F560796CDD031B668C3"
hx$=hx$&"17C1C43EB780ECB1D62D43C69395C3F65CCBC7"
hx$=hx$&"D1D3A1428F4ADFB3DD751C9CE75AC3BE63CAFC"
hx$=hx$&"8DA1C405C5DE3DDD9D21BC4E6AB107D738F3ED"
hx$=hx$&"1044505E8D68C865A36DA9B61D7C5F1DDF24FC"
hx$=hx$&"92D9C8DAA93632DB3F5F41AB29636B5E768A96"
hx$=hx$&"19A81B1A20AC6FEC6B48FFE7F0EC3F9ADF9A32"
hx$=hx$&"176E1BDEF677295FFFED8F3787E91778C67B27"
hx$=hx$&"86DAB1290CCC65058755DC0B9FB5740FAF836A"
hx$=hx$&"849FA27FBEA0B87E7093DD96BBB12B4F7BABD8"
hx$=hx$&"43F77254DE7881D3732717067B07EE9646355B"
hx$=hx$&"2951A56B52C8198A7C79103271E92862E208A2"
hx$=hx$&"E2FDEE477AC24CF117A3307EAD7016B12E022C"
hx$=hx$&"6636796D3B868D8589AFCF341A1E28B83BB802"
hx$=hx$&"15767B0FFC6215F6A2CC2A0DEBEAA38075A503"
hx$=hx$&"581BB327694BBC1E67560C98D52F282CE2337D"
hx$=hx$&"B7F9766E1142EE4EB3860F67E1DAA093974AC7"
hx$=hx$&"A8A647FCAAD9ABC386C28E89A9F690B24DF511"
hx$=hx$&"1D20A160B1E3C421DA82920848A95EAACC7F49"
hx$=hx$&"3E14FCDCB9858EF52896C6BA7EAF21EA0F8DF4"
hx$=hx$&"E7C6832762B8A191C7C1180E18C9E146F88783"
hx$=hx$&"BA366E768E8EA4AF093492131B4304A9208180"
hx$=hx$&"46325C586094383606DD5D40DDFD7467DD0022"
hx$=hx$&"093402CD49A021484623D14850AC803889E54B"
hx$=hx$&"1C210EC926165F1692F2A000F18E6E14A8075A"
hx$=hx$&"90407D90FCB9D39FCBAF46891C1920D3D19E28"
hx$=hx$&"9E60202E3200C924C921C804D0DD81EEFC6586"
hx$=hx$&"F8A60F46FC55221916213E8EC9972D67BAA7BB"
hx$=hx$&"B8F4DBB1BE727C7EEC04ED1C58E2DE712434F2"
hx$=hx$&"B32D4AE2B20F0E34A77CF3A09174677B50FC5F"
hx$=hx$&"936424D77F00512D9E15DD080000"

print "Creating file..."
option base 1
for t = 1 to len(hx$) step 2
  d$=chr$(dec(mid$(hx$,t,2)))
  file "davface.zip" write asc(d$)
next t

print "Unzipping..."
gunzip "davface.zip" to "davface.jpg"
file "davface.zip" delete
print "Done."

graphics
graphics clear 66/255,90/255,123/255
draw text "Touch to quit..." at 10,10
sprite "dav" load "davface.jpg"
sprite "dav" resize 300,300
sprite "dav" show
do
   x=rnd(screen_width())
   y=rnd(screen_height())
   sprite "dav" at rnd(x),rnd(y)
   pause .3
until touch_x(0)<> -1
sprite "dav" delete
text
end

User avatar
Mr. Kibernetik
Site Admin
Posts: 4786
Joined: Mon Nov 19, 2012 10:16 pm
My devices: iPhone, iPad, MacBook
Location: Russia
Flag: Russia

Re: Hexy: Add small binary files inside your code

Post by Mr. Kibernetik »

Nice idea!
In general, it is an advantage of interpreters - a program can create itself while running.

Post Reply