Ascii/Hex convert in bash

前端 未结 15 1464
广开言路
广开言路 2020-12-12 21:03

I\'m now doing it this way:

[root@~]# echo Aa|hexdump -v
0000000 6141 000a                              
0000003
[root@~]# echo -e \"\\x41\\x41\\x41\\x41\"
A         


        
相关标签:
15条回答
  • 2020-12-12 21:24

    Finally got the correct thing

    echo "Hello, world!" | tr -d '\n' | xxd -ps -c 200
    
    0 讨论(0)
  • 2020-12-12 21:28
    Text2Conv="Aa"
    for letter in $(echo "$Text2Conv" | sed "s/\(.\)/'\1 /g");do printf '%x' "$letter";done
    

    4161

    The trick is using sed to parse the Text2Conv to format we can then seperate anf loop using for.

    0 讨论(0)
  • 2020-12-12 21:28

    according to http://mylinuxbook.com/hexdump/ you might use the hexdump format parameter

    echo Aa | hexdump -C -e '/1 "%02X"'
    

    will return 4161

    to add an extra linefeed at the end, append another formatter.

    BUT: the format given above will give multiplier outputs for repetitive characters

    $ printf "Hello" | hexdump -e '/1 "%02X"' 
    48656C*
    6F
    

    instead of

    48656c6c6f
    
    0 讨论(0)
提交回复
热议问题