Generating C or Java byte array from a binary using command-line tools

Once in a while during development a software engineer needs to embed the binary object as byte array in C or Java language. While coding a solution is simple, it could be achieved by the following command line on Linux or cygwin:

For unsigned data:

od -v -t u1 <binary file> | cut -c9- | sed 's/\([0-9]\+\)/\1,/g'

For signed data (Java byte arrays):

od -v -t d1 <binary file> | cut -c9- | sed 's/\([0-9]\+\)/\1,/g'
This entry was posted in Uncategorized.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.