Which program creates a C array given any file?

前端 未结 5 1473
执念已碎
执念已碎 2020-12-07 17:33

I remember seeing in the past a program that would take any file and generate a C array representing that file as output; it would prevent distribution of a separate file in

相关标签:
5条回答
  • 2020-12-07 18:23

    For large files, converting to text and then making the compiler parse it all over again is inefficient and unnecessary. Use objcopy instead:

    objcopy -I binary -O elf32-i386 stuff stuff.o
    

    (Adjust the output architecture as necessary for non-x86 platforms.) Then once you link it into your program, you can access it like so:

    extern char _binary_stuff_start[], _binary_stuff_end[];
    #define SIZE_OF_STUFF (_binary_stuff_end - _binary_stuff_start)
    
    ...
    
    foo(_binary_stuff_start[i]);
    
    0 讨论(0)
  • 2020-12-07 18:29

    The easiest way is:

    xxd -i -a filename
    
    0 讨论(0)
  • 2020-12-07 18:31

    xxd -i

    0 讨论(0)
  • 2020-12-07 18:32

    I know this is Unix/Linux question, but anyone viewing this that wants to do the same in Windows can use Bin2H.

    0 讨论(0)
  • 2020-12-07 18:36
    hexdump -v -e '16/1 "0x%x," "\n"'
    

    would generate a C like array from stdin, but there is no declaration, no braces or good formatting.

    0 讨论(0)
提交回复
热议问题