Hexadecimal To Decimal in Shell Script

前端 未结 8 2211
北恋
北恋 2020-11-29 15:34

Can someone help me to convert a hexadecimal number to decimal number in a shell script?

E.g., I want to convert the hexadecimal number bfca3000 to deci

相关标签:
8条回答
  • 2020-11-29 16:18

    To convert from hex to decimal, there are many ways to do it in the shell or with an external program:

    With bash:

    $ echo $((16#FF))
    255
    

    with bc:

    $ echo "ibase=16; FF" | bc
    255
    

    with perl:

    $ perl -le 'print hex("FF");'
    255
    

    with printf :

    $ printf "%d\n" 0xFF
    255
    

    with python:

    $ python -c 'print(int("FF", 16))'
    255
    

    with ruby:

    $ ruby -e 'p "FF".to_i(16)'
    255
    

    with node.js:

    $ nodejs <<< "console.log(parseInt('FF', 16))"
    255
    

    with rhino:

    $ rhino<<EOF
    print(parseInt('FF', 16))
    EOF
    ...
    255
    

    with groovy:

    $ groovy -e 'println Integer.parseInt("FF",16)'
    255
    
    0 讨论(0)
  • 2020-11-29 16:21

    One more way to do it using the shell (bash or ksh, doesn't work with dash):

    echo $((16#FF))
    255
    
    0 讨论(0)
提交回复
热议问题