LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 02-14-2017, 12:13 PM   #1
fanoflq
Member
 
Registered: Nov 2015
Posts: 397

Rep: Reputation: Disabled
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.
 
Old 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: Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925
http://stackoverflow.com/questions/9...-obase-options
 
1 members found this post helpful.
Old 02-14-2017, 01:55 PM   #3
fanoflq
Member
 
Registered: Nov 2015
Posts: 397

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by hydrurga View Post
Thank you.
From link above:
"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?
 
Old 02-14-2017, 02:01 PM   #4
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
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.
Old 02-14-2017, 02:06 PM   #5
fanoflq
Member
 
Registered: Nov 2015
Posts: 397

Original Poster
Rep: Reputation: Disabled
Thank you.
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Inconsistent DNS...why? punt Linux - Networking 10 12-15-2014 04:57 PM
am not getting any result when am using 'if', without getting result for below script alavala53 Linux - General 3 10-25-2012 06:00 PM
[SOLVED] Grep for the result of a command within the result of another command jasonws Programming 6 11-18-2010 02:39 PM
Inconsistent Sound expatCM Linux - Hardware 18 07-12-2007 02:59 PM
inconsistent X server? rgiggs Mandriva 3 05-11-2004 10:32 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:12 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration