LinuxQuestions.org [SOLVED] bc: inconsistent result
 Linux - Newbie This Linux forum is for members that are new to Linux. Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

 02-14-2017, 12:13 PM #1 fanoflq Member   Registered: Nov 2015 Posts: 397 Rep: bc: inconsistent result Using bc utility to convert between binary and decimals. Code: ```Decimal to binary: #default input base is 10. ~ \$ echo "ibase=10;obase=2;254;" | bc #Convert 254 decimal to binary 11111110 ~ \$ echo "obase=2;ibase=10;254;" | bc 11111110 #Decimal-to-Binary, INPUT arguments order does not matter, but for Binary-To-Decimal, order matters for INPUT arguments! See below. Binary to decimal: ~ \$ echo "obase=10;ibase=2;1111;" | bc 15 #Correct output # Input order matters. Example incorrect output due to input order ~ \$ echo "ibase=2;obase=10;1111;" | bc 1111 # incorrect output``` Did I missed anything? Why does order matter in one case, and not the other? Is there a utility that converts IP4 address (dot decimal notation) into "dot" binary outputs? Thank you. Last edited by fanoflq; 02-14-2017 at 12:15 PM.
 02-14-2017, 12:19 PM #2 hydrurga LQ Guru   Registered: Nov 2008 Location: Pictland Distribution: Linux Mint 21 MATE Posts: 8,048 Blog Entries: 5 Rep: http://stackoverflow.com/questions/9...-obase-options 1 members found this post helpful.
02-14-2017, 01:55 PM   #3
fanoflq
Member

Registered: Nov 2015
Posts: 397

Original Poster
Rep:
Quote:
 Originally Posted by hydrurga http://stackoverflow.com/questions/9...-obase-options
Thank you.
"For multi-digit numbers, bc changes all input digits greater or equal to ibase to the value of ibase-1. "

Here I an example where all the digits are less than ibase.
Thus none of the input digits of obase or
input values (011 or 0111) exceeded ibase's value.
Thus there is no change to the input digits.
But the answers are still incorrect.

Code:
```~ \$ echo "ibase=2;obase=10;011" | bc
11           #I should get 3 decimal.
~ \$ echo "ibase=2;obase=10;0111" | bc
111         #I should get 7 decimal.```
What did I missed?

02-14-2017, 02:01 PM   #4
suicidaleggroll
LQ Guru

Registered: Nov 2010
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep:
You missed the line right above that one:
Quote:
 As soon as ibase=6 is interpreted, numbers are read in base 6.
This
Code:
```\$ echo "ibase=2;obase=10;011" | bc
11           #I should get 3 decimal.```
is not correct. As soon as ibase is interpreted, ALL numbers are read in base 2. That includes your "obase". So while you think you're setting obase to ten, you're setting it to binary "10", which is 2. You would need to set obase to ten base 2, or "1010", for it to be interpreted correctly in this order:
Code:
```\$ echo "ibase=2;obase=1010;011" | bc
3
\$ echo "ibase=2;obase=1010;0111" | bc
7
\$ echo "ibase=2;obase=1010;1111" | bc
15```

Last edited by suicidaleggroll; 02-14-2017 at 02:04 PM.

2 members found this post helpful.
 02-14-2017, 02:06 PM #5 fanoflq Member   Registered: Nov 2015 Posts: 397 Original Poster Rep: Thank you.

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is Off HTML code is Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post punt Linux - Networking 10 12-15-2014 04:57 PM alavala53 Linux - General 3 10-25-2012 06:00 PM jasonws Programming 6 11-18-2010 02:39 PM expatCM Linux - Hardware 18 07-12-2007 02:59 PM rgiggs Mandriva 3 05-11-2004 10:32 PM

LinuxQuestions.org

All times are GMT -5. The time now is 06:09 PM.

 Contact Us - Advertising Info - Rules - Privacy - LQ Merchandise - Donations - Contributing Member - LQ Sitemap -