Immediate Help: Convert Binary Num. to Decimal

  • Thread starter Thread starter remaan
  • Start date Start date
Click For Summary

Discussion Overview

The discussion revolves around the problem of determining whether a user-input number is binary and converting it to decimal. It includes considerations of programming logic, user input validation, and the nature of binary versus decimal representations.

Discussion Character

  • Homework-related
  • Debate/contested
  • Technical explanation

Main Points Raised

  • One participant suggests checking if the input number contains only 0s and 1s, questioning the complexity of implementing this with an if statement.
  • Another participant proposes converting the integer to a string and using a for loop to check if any digit is above 1 to determine if it is binary.
  • It is noted that numbers like 1010101 can also be valid decimal numbers, raising ambiguity in classification.
  • A participant argues that humans cannot easily distinguish between binary and decimal representations without explicit user input, suggesting the need for user clarification.
  • Another reply discusses the statistical likelihood of a random decimal number containing only 0s and 1s, proposing that a marker (like a percent sign) could be used to indicate binary numbers.

Areas of Agreement / Disagreement

Participants express differing views on how to validate binary input and the implications of numbers that can be interpreted as both binary and decimal. No consensus is reached on the best approach to the problem.

Contextual Notes

Participants highlight the complexity of distinguishing between binary and decimal representations, emphasizing the need for user input clarification and the potential for ambiguity in number representation.

remaan
Messages
132
Reaction score
0

Homework Statement



In this problem I need to check if the input num. from the useer is a binary or Not.
And, then I need to convert this Binary Num. to Decimal.





The Attempt at a Solution



Now, for a number to be a Binary it should only contains 0s and 1s,
should I make an if statement ?
But, even if I make this if it will be so complex, because I have to check that every num. except 0 and 1 does not exist ?

Or, there is a method to check that ?
 
Last edited:
Physics news on Phys.org
you could put the integer into a string then use a for loop to cycle through the array and check to see if the value is above 1. Then you will know if it is binary or not.

you may also want to look at this page http://www.cplusplus.com/reference/clibrary/cstdlib/itoa/
 
Binary numbers are not the only ones that contain only 1s and 0s.

The number 1010101 is a perfectly valid Decimal number.
 
that is true, but even humans cannot distinguish between "1010101" and "ten million one hundred and one thousand and one" in integer form. So there is no way to program that with 0% error unless you ask the user if the input is a binnary number or an integer.
 
Thanks a lot!

I will check this =)
 
That is right.
A number with just ones and zeros can occur in a string of decimal numbers.

You can work out the odds of this happening.
Each digit has to be a 1 or a zero and not a number between 2 and 9, inclusive.

So there is a 20% chance of this happening once, a 4 % chance of it happening twice and so on.
If you took the first 10 digits and they were all 0s or 1s the odds against this happening randomly in a decimal number would be nearly 10 million to 1.

To be 100% sure, though, the data should be tagged with some marker that it is binary.
It is traditional to use the percent sign as a tag to indicate binary. So, you would say %11111 = 31
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
18K
Replies
3
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 2 ·
Replies
2
Views
5K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K