Hexadecimal To Decimal in Shell Script

前端 未结 8 2273
北恋
北恋 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:12

    I have this handy script on my $PATH to filter 0x1337-like; 1337; or "0x1337" lines of input into decimal strings (expanded for clarity):

    #!/usr/bin/env bash
    
    while read data; do
      withoutQuotes=`echo ${data} | sed s/\"//g`
      without0x=`echo ${withoutQuotes} | sed s/0x//g`
      clean=${without0x}
      echo $((16#${clean}))
    done
    

提交回复
热议问题