Monday, January 17, 2011

Binary to Decimal conversation

I've been fiddling with bits and needed to convert a set of large  binary string to decimal. In my first attempt, I applied Excel formula BIN2DEC  - worked great, but for some reason it didn't with more than 12 bits. I am not sure whether there is some sort of limitation or perhaps I missed something, but I've started to look for other ways to do the same thing.

I've found answer on this  post, in particular in one the comments that suggested to use dc - an arbitrary precision calculator:

Here is how you would convert binary to decimal it in linux:

echo "2 i BINARY BITS p" | dc

example:
echo "2 i 11000000111001 p" | dc

and decimal to binary
echo "2 o DECIMAL NUMBER p" | dc
example
echo "2 o 12345 p" | dc

That's it! Thanks to Vocho Amerillo