feel a bit stupid asking this (sounds as basics) but can't find an answer elsewhere. I want to convert binary data to hex, just that, no fancy formatting and all. hexdump
seems too clever, it "overformats" for me. I want to take x bytes from the /dev/random and pass them on as hex.
Preferably I'd like to use only standard linux tools, so that I don't need to install it on every machine (there are many)
With od (GNU systems):
With hexdump (BSD systems):
From http://en.wikipedia.org/wiki/Hex_dump#od_and_hexdump:
"Depending on your system type, either or both of these two utilities will be available--BSD systems deprecate od for hexdump, GNU systems the reverse."
Watch out!
hexdump
andxxd
give the results in different endianness!http://geekandpoke.typepad.com/.a/6a00d8341d3df553ef01543533e604970c-800wi :D
dd + hexdump will also work:
If you need a large stream (no newlines) you can use
tr
andxxd
(part of vim) for byte-by-byte conversion.Or you can use
hexdump
(posix) for word-by-word conversion.Note that the difference is endianness.
Perhaps use
xxd
:All the solutions seem to be hard to remember or too complex, I find using
printf
the shortest one:[edit]
But as noted in comments, this is not what author wants, so to be fair below is full answer.
... to use above to output actual binary data stream:
what it does:
printf '%x,' 1 2 3
, will print1,2,3,
cat /dev/urandom
- it outputs random binary datahead -c 5
- limits binary data to 5 bytesod -An -vtu1
- octal dump command, converts binary to decimalas a testcase ('a' is 61 hex, 'p' is 70 hex, ...):
or to test individual binary bytes, on input lets give 61 decimal ('=' char) to produce binary data (
'\\x%x'
format does it), above command will output correctly 3d (decimal 61):