Pages

Monday, May 31, 2010

Terminal. Hexadecimal to Decimal

Bash (Born Again SHell) allows to convert hexadecimal number to decimal integers. For example, so:
echo $[0xFFFE]
Or this way:
let x=0xFFFE ; echo $x
or
((x=0xFFFE)) ; echo $x
Or so:
printf "%d\n" 0xFFFE
The last way, allows to convert the decimal values to the hexadecimal format. So I made two aliases for a test:
alias h2d='printf "%d\n" ${1}'
alias d2h='printf "0x%x\n" ${1}'
Now, I can write:
h2d 0x3B
and
d2x 12

No comments:

Post a Comment