PDA

View Full Version : Bin to C



Applewood
10-13-2004, 05:42 AM
Hi.

Anyone got a small widget to take a given file and save it back out as a const static array of numbers suitable for inclusion into source code ?

I need to embed a few things in a library and I figure this is the best way of doing it.

Yes, I know I could write something in about an hour to do it, but I'm sure someones beat me to it :)

Thanks....

Diodor Bitan
10-13-2004, 06:02 AM
This Lua program seems to work:


io.output ("out.c")

table.foreach ({a="a.dat", b="b.dat"},
function (var, file)
local inp = assert(io.open(file, "rb"))
local data = inp:read("*all")
data = string.gsub(data, ".",
function (ch) return string.byte (ch) .. "," end)
data = string.sub (data, 1, string.len (data) - 1)
data = string.gsub (data, "("..string.rep (",[^,]*", 8)..")",
function (s) return s .. "\n" end)
io.write(string.format ("const unsigned char %s[] = {\n%s}\n\n", var, data))
end)

io.close ()

To use, just add lua.exe in the same directory as the files, copy the script above in a "script.lua" file and call "lua.exe script.lua". Change the first line to generate output in the c file you like and decide what files to include by adding entries in the table on the second code line in the format varname="filename.ext"

It should be easy to set MSVC to automatically call the script during compile time.

Applewood
10-13-2004, 06:21 AM
Thanks man, but I'd prefer not to depend on Lua. In my development environment, Lua being installled isn't a given and I'd like to keep it easy to call (from a command line or recompileable c source).

Sorry, but thanks anwyays..

Wayward
10-13-2004, 06:26 AM
http://gnu.de.uu.net/b2c/
http://www.sm5sxl.net/~mats/src/dos/BIN2C.C
BIN2C V1.0 (http://www.programmersheaven.com/zone3/cat156/17063.htm)
http://www.sm5sxl.net/~mats/src/unix/misc/bin2c.c

Diodor Bitan
10-13-2004, 10:04 AM
Original post by Applewood
Thanks man, but I'd prefer not to depend on Lua. In my development environment, Lua being installled isn't a given and I'd like to keep it easy to call (from a command line or recompileable c source).

Sorry, but thanks anwyays..

I half expected that, even though it's just getting the lua.exe file for Windows platforms. Just testing the "scripting language productivity" theory.

Applewood
10-13-2004, 04:50 PM
lol :)

I went for maximum productivity and found a link to a compiled C program on the net. Job done !

Thanks for the links above, Wayward. I realised I was being lazy and googled meself right after posting and found loads of em before you even read this.

Sorry to be bone idle guys :p