Conversion hex string into ascii in bash command line

前端 未结 7 1484
孤独总比滥情好
孤独总比滥情好 2020-12-12 23:43

I have a lot of this kind of string and I want to find a command to convert it in ascii, I tried with echo -e and od, but it did not work.

7条回答
  •  庸人自扰
    2020-12-13 00:22

    The values you provided are UTF-8 values. When set, the array of:

    declare -a ARR=(0xA7 0x9B 0x46 0x8D 0x1E 0x52 0xA7 0x9B 0x7B 0x31 0xD2)
    

    Will be parsed to print the plaintext characters of each value.

    for ((n=0; n < ${#ARR[*]}; n++)); do echo -e "\u${ARR[$n]//0x/}"; done
    

    And the output will yield a few printable characters and some non-printable characters as shown here:

    For converting hex values to plaintext using the echo command:

    echo -e "\x"
    

    And for converting UTF-8 values to plaintext using the echo command:

    echo -e "\u"
    

    And then for converting octal to plaintext using the echo command:

    echo -e "\0"
    

    When you have encoding values you aren't familiar with, take the time to check out the ranges in the common encoding schemes to determine what encoding a value belongs to. Then conversion from there is a snap.

提交回复
热议问题