Convert bytes to source code
About
bin2src
takes an input stream of bytes and converts
that into source code. That source code version of the
input can then be easily embedded into your project with Copy+Paste.
Usage
Read bytes from STDIN and write them to STDOUT:
$ echo "hello" | bin2src
\x68\x65\x6c\x6c\x6f\x0a
Note that there is no need to convert your input bytes to a plain text encoding:
$ head -c 10 /dev/urandom | bin2src
\x30\xc7\x18\x67\x5b\xc3\x7b\x7d\x12\xcd
The primary purpose of bin2src
is to facilitate embedding binary blobs into software source code.
To specify an output format, use the -f
option ("f" for format):
$ head -c 10 /dev/urandom | bin2src -f python
DATA = """\x22\x82\x2d\xd4\xb8\x8c\x36\xb4\x35\x21"""
If you would like to change the variable name, use the -a
option ("a" is a common mathematical variable):
$ head -c 10 /dev/urandom | bin2src -f python -a RAND_BYTES
RAND_BYTES = """\xf5\xde\x5e\xdc\x66\xeb\x89\x24\x13\xd5"""
Other options are available. See the full list by running bin2src --help
.
Supported output formats
- plaintext (default)
- go
- rust
- python