Linux C在shell脚本中将二进制数据转换为十六进制

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6292645/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 01:03:52  来源:igfitidea点击:

Cconvert binary data to hexadecimal in a shell script

linuxscriptinghexdump

提问by davka

I want to convert binary data to hexadecimal, just that, no fancy formatting and all. hexdumpseems too clever, and it "overformats" for me. I want to take x bytes from the /dev/random and pass them on as hexadecimal.

我想将二进制数据转换为十六进制,仅此而已,没有花哨的格式等等。hexdump似乎太聪明了,它对我来说“过度格式化”。我想从 /dev/random 中取出 x 个字节并将它们作为十六进制传递。

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

我最好只使用标准的 Linux 工具,这样我就不需要在每台机器上都安装它(有很多)。

采纳答案by unutbu

Perhaps use xxd:

也许使用xxd

% xxd -l 16 -p /dev/random
193f6c54814f0576bc27d51ab39081dc

回答by Blagovest Buyukliev

Perhaps you could write your own small tool in C, and compile it on-the-fly:

也许您可以用 C 编写自己的小工具,然后即时编译它:

int main (void) {
  unsigned char data[1024];
  size_t numread, i;

  while ((numread = read(0, data, 1024)) > 0) {
    for (i = 0; i < numread; i++) {
      printf("%02x ", data[i]);
    }
  }

  return 0;
}

And then feed it from the standard input:

然后从标准输入中输入它:

cat /bin/ls | ./a.out

You can even embed this small C program in a shell script using the heredoc syntax.

您甚至可以使用 heredoc 语法将这个小 C 程序嵌入到 shell 脚本中。

回答by Blagovest Buyukliev

dd + hexdump will also work:

dd + hexdump 也可以工作:

dd bs=1 count=1 if=/dev/urandom 2>/dev/null  | hexdump -e '"%x"'

回答by Zibri

Watch out!

小心!

hexdumpand xxdgive the results in different endianness!

hexdumpxxd以不同的字节顺序给出结果!

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

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

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

回答by H?kon A. Hjortland

With od (GNU systems):

使用 od(GNU 系统):

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

With hexdump (BSD systems):

使用 hexdump(BSD 系统):

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

来自http://en.wikipedia.org/wiki/Hex_dump#od_and_hexdump
“根据您的系统类型,这两个实用程序中的一个或两个都可用--BSD 系统弃用 od 为 hexdump,而 GNU 系统则相反。”

回答by Kevin Cox

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

如果您需要大流(无换行符),您可以使用trxxd(vim 的一部分)进行逐字节转换。

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

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

或者您可以使用hexdump(posix) 进行逐字转换。

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

Note that the difference is endianness.

请注意,区别在于字节序。

回答by Kevin Cox

This three commands will print the same (0102030405060708090a0b0c):

这三个命令将打印相同的 (0102030405060708090a0b0c):

n=12
echo "$a" | xxd -l "$n" -p
echo "$a" | od  -N "$n" -An -tx1 | tr -d " \n" ; echo
echo "$a" | hexdump -n "$n" -e '/1 "%02x"'; echo

Given that n=12and $ais the byte values from 1 to 26:

鉴于此,n=12and$a是从 1 到 26 的字节值:

a="$(printf '%b' "$(printf '\0%o' {1..26})")"

That could be used to get $nrandom byte values in each program:

这可用于$n在每个程序中获取随机字节值:

xxd -l "$n" -p                   /dev/urandom
od  -vN "$n" -An -tx1            /dev/urandom | tr -d " \n" ; echo
hexdump -vn "$n" -e '/1 "%02x"'  /dev/urandom ; echo

回答by marcinj

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

所有的解决方案似乎都很难记住或太复杂,我发现使用printf最短的一个:

$ 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
  • printf '%x\n' .... - 打印整数序列,即。printf '%x,' 1 2 3, 将打印1,2,3,
  • $(...) - 这是一种获取某些 shell 命令的输出并对其进行处理的方法
  • cat /dev/urandom- 它输出随机二进制数据
  • head -c 5- 将二进制数据限制为 5 个字节
  • od -An -vtu1- 八进制转储命令,将二进制转换为十进制

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

作为测试用例('a' 是 61 十六进制,'p' 是 70 十六进制,...):

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

或者要测试单个二进制字节,在输入上让我们给出 61 位十进制数 ('=' char) 来生成二进制数据('\\x%x'format 这样做),上面的命令将正确输出 3d(十进制 61):

$printf '%x\n' $(echo -ne "$(printf '\x%x' 61)" | head -c 5 | od -An -vtu1)
3d