Can xxd be used to output the binary representation of hex number , not a string?
Solution 1
echo '0A'
produces three characters: 0 A NL; xxd -b
will then print those three characters in binary. If you wanted just the single byte whose value is 10 (i.e. hexadecimal A
), you could write (in bash):
echo -n $'\x0A'
^ ^ ^
| | |
| | +-- `\x` indicates a hexadecimal escape
| +----- Inside a $' string, escapes are interpreted
+------- -n suppresses the trailing newline
A better alternative would be printf '\x0A'
; printf interprets escape sequences in the format string, and does not output implicit newlines. (For a completely Posix-compatible solution, you would need an octal escape: printf '\012'
. printf
should work on any Posix-compatible shell but hexadecimal escapes are an extension.) Yet another bash possibility is echo -n -e '\x0A'
; the (non-standard) -e
flag asks echo to interpret escape sequences.
echo '0A' | xxd -b
won't output the equivalent of hex 0A
, because xxd
doesn't know that you intend 0A
to be a hex number rather than two characters. It just takes its input as a series of characters, regardless of what those characters are.
Endianness does not affect bytes. The order of bits inside a byte is entirely conceptual until the byte is transmitted over a serial line and even then it is only visible with an oscilloscope or something similar.
Solution 2
If you want the binary output for a string of hex digits, xxd -r -p
. E.g.:
$ echo '0A0B0C0D' | xxd -r -p | xxd -b
0000000: 00001010 00001011 00001100 00001101 ....
converts 0A0B0C0D into a four bytes of binary (first call to xxd), and then converts it back to be printable (second call). You say you want a binary output, but the examples you're trying for are a printable representation.
I don't know of anything where endianness is ambiguous at the nibble level, as you imply in your second example. The conversion in xxd is a pure byte at a time, not assuming they represent any particular multi-byte number.
Solution 3
Yes, you can use xxd
to do that, but you may not like the input format. From your example, 0x0a is the hex value of an ASCII newline. You can create a file with a single newline in it, then use xxd
to create the listing:
$ xxd one_line_file
0000000: 0a .
You can get a single newline out of xxd
from that output if you give it the proper flags:
$ echo "0000000: 0a" | xxd -r > another_one_line_file
$ xxd another_one_line_file
0000000: 0a
xxd
has a pretty nice output format, I don't think it would be too onerous to create your packets using a text editor, then use xxd -r
to go to binary.
.
Related videos on Youtube
TheMeaningfulEngineer
I like to think of myself as a Hardware/Software guy who will gladly discuss referential transparency during a code review and the next moment take a circular saw to build a casing for a self made power supply. My main interest can be summarized into Linux related software development, low power electronics and general DIY projects.
Updated on September 18, 2022Comments
-
TheMeaningfulEngineer over 1 year
I would like to generate a raw binary packet from hex in the command line for the purpose of testing a protocol. I'm hoping its simpler to do it this way than writing an aplication.
Here is what currently happens:
echo '0A' | xxd -b 0000000: 00110000 01000001 00001010
What i would like to have is:
echo '0A' | xxd -b 0000000: 00001010
or
echo '0A' | xxd -b 0000000: 01010000
Since I'm not jet sure about the required endianess. I would like to be able to change it when necessary. Is this achievable?
-
TheMeaningfulEngineer about 10 yearsSorry about the
50
. It's a typo. Fixing it now