Sunday, April 20, 2008

Quick hex / decimal conversion using CLI

Once in a while, you need to convert a number from hexadecimal to decimal notation, and vice versa.

Say, you want to know the decimal equivalent of the hexadecimal 15A.

You can convert in many different ways, all within bash, and relatively easy.

To convert a number from hexadecimal to decimal:

 $ echo $((0x15a))
346


 $ printf '%d\n' 0x15a
346


 $ perl -e 'printf ("%d\n", 0x15a)'
346


 $ echo 'ibase=16;obase=A;15A' | bc
346


Note that ibase and obase specify the input and the output notation respectively.
By default, the notation for both is decimal unless you change it using ibase or obase.

Because you change the notation to hex using ibase, your obase needs to be specified in hex (A in hex = 10 in decimal).

The input number (15A) needs to be in UPPER case. 15a will give you a parse error.


To convert from decimal to hex,
$ printf '%x\n' 346
15a


 $ perl -e 'printf ("%x\n", 346)'
15a


 $ echo 'ibase=10;obase=16;346' | bc
15A

15 comments:

  1. how do you convert in file?

    ReplyDelete
  2. Hey this is adithya.

    You can use perl to print in binary also.

    perl -e 'printf "%b\n", 10'
    1010

    perl -e 'printf "%d\n", 0b1010'
    10

    ReplyDelete
  3. Adithya again..

    To convert in a file, follow the steps.

    1. open file in vim
    2. type :echo printf('%x',1024)
    will print 400 as hex.

    3. type :echo 0x110 will print its decimal value immediately. no need of printf

    ReplyDelete
  4. You can do math (integers only) and conversion directly with bash and printf. You can even mix hex and decimal numbers:

    printf "%x" $[0x5 + 0x5 + 10]

    printf "%d" $[0x5 + 0x5 + 10]


    To convert hex in dec you can use only echo, if you want to:

    echo $[0x5 + 0x5 + 10]

    Regards

    ReplyDelete
  5. a script named '?' calculate and convert binary, decimal and hex:

    #! /bin/bash
    #
    [[ -z "$1" ]] && echo -e "32bit calc with perl\nusage: `basename $0` _ | 0x_ | 0b_" && exit

    Cmd="printf \"%d 0x%x 0b%b\\n\",$1,$1,$1"

    perl -e "$Cmd"

    example:
    /usr/local/bin$ ? 0b11+0xff+99
    357 0x165 0b101100101

    SoftICE live! ;-)

    ReplyDelete
  6. new revision with octal and float:

    #!/bin/bash
    #
    [ -z "$1" ] && printf "32bit calc with perl\nusage: `basename $0` _ | 0x_ | 0b_ | 0_ \n" && exit

    perl -e "printf \"%d %f 0x%x 0b%b 0%o \\n\",$1,$1,$1,$1,$1"

    example:
    ~$ ? 13579+0xACE+0b10101+01357
    17117 17117.000000 0x42dd 0b100001011011101 041335

    ReplyDelete
  7. How can you do this for signed numbers?
    Such as:
    echo $((0xffffff9f)) gives me 4294967199 but i am looking for the output -97

    ReplyDelete
  8. Dear Sachin!

    You should write this:
    echo $(( ~0xff | 0x9f ))
    I think you have a 64bit machine, so you have to set on all of the high bits!
    You can write this too:
    echo $(( 0xffffffffffffff9f ))
    The first one is more general, works on 32bit and 64bit machines as well.

    Have a nice day!
    David

    ReplyDelete
  9. Nice one!

    I used it to create bash functions :)

    ReplyDelete
  10. Thank you very much for this inspirational info !!!

    ReplyDelete