convert binary data to hex in shell script?

2020-02-17 04:53发布

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)

8条回答
在下西门庆
2楼-- · 2020-02-17 05:16

With od (GNU systems):

$ echo abc | od -A n -v -t x1 | tr -d ' \n'
6162630a

With hexdump (BSD systems):

$ echo abc | hexdump -ve '/1 "%02x"'
6162630a

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

查看更多
Ridiculous、
3楼-- · 2020-02-17 05:22

Watch out!

hexdump and xxd give the results in different endianness!

$ echo -n $'\x12\x34' | xxd -p
1234
$ echo -n $'\x12\x34' | hexdump -e '"%x"'
3412

http://geekandpoke.typepad.com/.a/6a00d8341d3df553ef01543533e604970c-800wi :D

查看更多
走好不送
4楼-- · 2020-02-17 05:23

dd + hexdump will also work:

dd bs=1 count=1 if=/dev/urandom 2>/dev/null  | hexdump -e '"%x"'
查看更多
霸刀☆藐视天下
5楼-- · 2020-02-17 05:25

If you need a large stream (no newlines) you can use tr and xxd (part of vim) for byte-by-byte conversion.

head -c1024 /dev/urandom | xxd -p | tr -d $'\n'

Or you can use hexdump (posix) for word-by-word conversion.

head -c1024 /dev/urandom | hexdump '-e"%x"'

Note that the difference is endianness.

查看更多
相关推荐>>
6楼-- · 2020-02-17 05:26

Perhaps use xxd:

% xxd -l 16 -p /dev/random
193f6c54814f0576bc27d51ab39081dc
查看更多
迷人小祖宗
7楼-- · 2020-02-17 05:26

All the solutions seem to be hard to remember or too complex, I find using printf the shortest one:

$ printf '%x\n' 256
100

[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:

printf '%x\n' $(cat /dev/urandom | head -c 5 | od -An -vtu1)

what it does:

  • printf '%x\n' .... - prints a sequence of integers , ie. printf '%x,' 1 2 3, will print 1,2,3,
  • $(...) - this is a way to get output of some shell command and process it
  • cat /dev/urandom - it outputs random binary data
  • head -c 5 - limits binary data to 5 bytes
  • od -An -vtu1 - octal dump command, converts binary to decimal

as a testcase ('a' is 61 hex, 'p' is 70 hex, ...):

$ printf '%x\n' $(echo "apple" | head -c 5 | od -An -vtu1)
61
70
70
6c
65

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):

$printf '%x\n' $(echo -ne "$(printf '\\x%x' 61)" | head -c 5 | od -An -vtu1)
3d
查看更多
登录 后发表回答