bash ascii到hex

use*_*261 17 bash hex ascii

我想知道是否有人可以帮助我在bash中将ascii转换为hex.示例代码:

#!/bin/bash 
STR = "hello"
#Convert to hex
HEXVAL = $STR #(in hex here?)
Run Code Online (Sandbox Code Playgroud)

我希望hexval具有值:68656C6C6F(十六进制你好)

Joh*_*ica 28

$ STR="hello"
$ HEXVAL=$(xxd -pu <<< "$STR")
$ echo "$HEXVAL"
6C6C6568A6F
Run Code Online (Sandbox Code Playgroud)

要么:

$ HEXVAL=$(hexdump -e '"%X"' <<< "$STR")
$ echo "$HEXVAL"
6C6C6568A6F
Run Code Online (Sandbox Code Playgroud)

小心'"%X"'; 它有单引号和双引号.


mrc*_*mpe 7

你有几个选择

$ printf hello | xxd
0000000: 6865 6c6c 6f                             hello
Run Code Online (Sandbox Code Playgroud)

另请参见: 使用bash进行Ascii/Hex转换