Can xxd be used to output the binary representation of hex number , not a string?

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.


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.


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. .