Using hexdump to represent a binary file in Java source code

To embed a binary file (e.g. a png image) into Java source code:

$ hexdump -v -e '/1 "%3i, "' myfile.png
-119,  80,  78,  71,  13,  10,  26,  10,   0,   0,   0,  13,  73,  72,  68,  82, ...

Copy and paste the output into Java code like this:

private static final byte[] myfile = { (byte)-119,  80,  78,  71, ... };

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.