bash ascii to hex

前端 未结 5 1938
野的像风
野的像风 2020-12-28 16:45

was wondering if anyone could help me with converting ascii to hex in bash. Example code:

#!/bin/bash 
STR = \"hello\"
#Convert to hex
HEXVAL = $STR #(in hex         


        
5条回答
  •  隐瞒了意图╮
    2020-12-28 17:13

    $ str="hello"
    $ hex=$(xxd -pu <<< "$str")
    $ echo "$hex"
    6C6C6568A6F
    

    Or:

    $ hex=$(hexdump -e '"%X"' <<< "$str")
    $ echo "$hex"
    6C6C6568A6F
    

    Careful with the '"%X"'; it has both single quotes and double quotes.

提交回复
热议问题