How to convert hexadecimal BC to decimal?
How do I convert Hex to Decimal?
Why would I need to convert hex to decimal?
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:
$ node -e "console.log(parseInt('FF', 16))"
255
with rhino:
$ rhino -e "print(parseInt('FF', 16))"
255
with groovy:
$ groovy -e 'println Integer.parseInt("FF",16)'
255
Dealing with a very lightweight embedded version of busybox on Linux means many of the traditional commands are not available (bc, printf, dc, perl, python)
echo $((0x2f))
47
hexNum=2f
echo $((0x${hexNum}))
47
Credit to Peter Leung for this solution.
What you actually want to say is:
$ echo "ibase=16; C0" | bc
192
for hex-to-decimal, and:
$ echo "obase=16; 192" | bc
C0
for decimal-to-hex.
You don't need to give both ibase and obase for any conversion involving decimal numbers, since these settings default to 10.
You do need to give both for conversions such as binary-to-hex. In that case, I find it easiest to make sense of things if you give obase first:
$ echo "obase=16; ibase=2; 11000000" | bc
C0
If you give ibase first instead, it changes the interpretation of the following obase setting, so that the command has to be:
$ echo "ibase=2; obase=10000; 11000000" | bc
C0
This is because in this order, the obase value is interpreted as a binary number, so you need to give 10000โ=16 to get output in hex. That's clumsy.
Now letโs work out why your three examples behave as they do.
echo "ibase=F;obase=A;C0" | bc180That sets the input base to 15 and the output base to 10, since a single-digit value is interpreted in hex, according to POSIX. This asks
bcto tell you what C0โโ is in base Aโโ =10, and it is correctly answering 180โโ, though this is certainly not the question you meant to ask.echo "ibase=F;obase=10;C0" | bcC0This is a null conversion in base 15.
Why? First, because the single
Fdigit is interpreted in hex, as I pointed out in the previous example. But now that you've set it to base 15, the following output base setting is interpreted that way, and 10โโ =15, so you have a null conversion from C0โโ to C0โโ .That's right, the output isn't in hex as you were assuming, it's in base 15!
You can prove this to yourself by trying to convert
F0instead ofC0. Since there is noFdigit in base 15,bcclamps it toE0, and givesE0as the output.echo "ibase=16; obase=A; C0"192This is the only one of your three examples that likely has any practical use.
It is changing the input base to hex first, so that you no longer need to dig into the POSIX spec to understand why
Ais interpreted as hex, 10 in this case. The only problem with it is that it is redundant to set the output base to Aโโ=10, since that's its default value.
Setting ibase means you need to set obase in that same base. Explaining your examples will show this:
echo "ibase=F;obase=A;C0" | bc
You set bc to consider input numbers as represented in base 15 with the "ibase=F". "obase=A" sets output numbers to base 10, which is the default.
bc reads C0 as a base 15 number: C = 12. 12*15 = 180.
echo "ibase=F;obase=10;C0" | bc
In this one, you set input to base 15, and output to 10 - in base 15, so output base is 15. C0 input in base 15 is C0 output in base 15.
echo "ibase=16;obase=A;C0" | bc
Set input to base 16, output to base 10 (A in base 16 is 10 in base 10).
C0 converted to base 10 is: 12*16 = 192
My personal rule is to set obase first, so that I can use base 10. Then set ibase, also using base 10.
Note that bc does have an ironic exception: ibase=A and obase=A always sets input and output to base 10. From the bc man page:
Single digit numbers always have the value of the digit
regardless of the value of ibase.
This behavior is enshrined in the specification of bc: From the 2004 OpenGroup bc specification:
When either ibase or obase is assigned a single digit value from
the list in 'Lexical Conventions in bc', the value shall be assumed
in hexadecimal. (For example, ibase=A sets to base ten, regardless
of the current ibase value.) Otherwise, the behavior is undefined
when digits greater than or equal to the value of ibase appear in
the input.
That's why the ibase=F setting changed your input base to base 15, and why I recommended to always set the base using base 10. Avoid confusing yourself.
It's not just that number, it's any number over 7fffffffffffffff, because it's using 64-bit integers and that's the largest one. 16-digit numbers over that wrap around and become negative (because of two's complement representation of signed integers):
$ echo $((16#7fffffffffffffff))
9223372036854775807
$ echo $((16#7fffffffffffffff + 1))
-9223372036854775808
$ echo $((16#8000000000000000))
-9223372036854775808
Past ffffffffffffffff (aka -1), it wraps back to zero:
$ echo $((16#ffffffffffffffff))
-1
$ echo $((16#ffffffffffffffff + 1))
0
$ echo $((16#10000000000000000))
0
Net result: only the last 16 hex digits actually matter; anything past that gets dropped off the high end of the 64-bit integer representation:
$ echo $((16#0000000000000010))
16
$ echo $((16#10000000000000010))
16
$ echo $((16#ffffffff0000000000000010))
16
Since 1553255926290448384 is 17 digits long, the first digit is being dropped off in this way:
$ echo $((16#1158e460913d00000))
1553255926290448384
$ echo $((16#158e460913d00000))
1553255926290448384
$ echo $((16#121345158e460913d00000))
1553255926290448384
You can do what you are looking to do using bc as a base conversion with a short script:
#!/bin/bash
## validate sufficient input
test -n "$1" || {
printf "\n error: insufficient input. usage: %s num [obase (2)] [ibase (10)]\n\n" "${0//*\//}"
exit 1
}
## test for help
test "$1" = "-h" || test "$1" = "--help" && {
printf "\n usage: %s num [obase (2)] [ibase (10)] -- to convert number\n\n" "${0//*\//}"
exit 0
}
## validate numeric value given for conversion (bash only test)
ival="${1^^}"
[[ $ival =~ [^0-9A-F] ]] && {
printf "\n error: invalid input. Input must be within upper/lower case hex character set [0-9A-Fa-f]\n\n"
exit 1
}
ob=${2:-2}
ib=${3:-10}
# set obase first before ibase -- or weird things happen.
printf "obase=%d; ibase=%d; %s\n" $ob $ib $ival | bc
Example Use/Output
$ bash hex2dec.sh -h
usage: hex2dec.sh num [obase (2)] [ibase (10)] -- to convert number
Using your example:
$ bash hex2dec.sh 1158e460913d00000 10 16
20000000000000000000
Also handy if you want it in binary as well:
$ bash hex2dec.sh 1158e460913d00000 2 16
10001010110001110010001100000100100010011110100000000000000000000