Public Group

#### Archived

This topic is now archived and is closed to further replies.

# Translating Binary to decimal troubles

This topic is 5425 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I''m getting strange output of the translated binary string. Here''s my source:
void BinaryToDecimal()
{
string binary = " ";
int    i, j;
int    decimal = 0;

cout << "Please input a binary string of 0''s and 1''s to convert to a decimal number:	";
cin  >> binary;

for (i = 0; i < binary.length(); i++)
{
if (binary[i] == ''1'' || binary[i] == ''0'')
{
for (j = binary.length(); j >= 0; j--)
decimal = (binary[j] * (pow(2,j)));
}
else if (binary[i] != ''0'')
}
cout << endl << decimal;
}

That''s the command, run it and just insert 1. It should say 1 but it gives me 49. What''s wrong with it?
R.I.P. Mark Osback Solo Pa Mi Gente VG-Force

##### Share on other sites
49 is the ASCII code for the character ''1'' so technically it is correct.

You can look at an ASCII table
Here

Clippy:"OMG, A NUMBAR! Let me format it for you"
Unsuspecting Student: "Ahhhhh! Damn you paperclip you ruined my paper. A thousand deaths upon you!"

"Game Programming: Without programming you''ve just got game"

##### Share on other sites
I want a numerical value though.

R.I.P. Mark Osback
Solo Pa Mi Gente
VG-Force

##### Share on other sites
Then you have to offset the character value like someone else explained in another thread you had, something like this although it isnt very pretty. I should also point out that the way you have things now it will only work it your input string is of length 1. For example if you input something like "1010" your program is only going to return 1 because you overwrite the value of decimal each time instead of keeping a running total. I can see some other issues too but I leave it to you to figure it out

for (j = binary.length(); j >= 0; j--)   //binary[j]-'0' will return a number between 0 and 9   //assuming binary[j] is a character for a number   decimal = ( (binary[j]-'0') * (pow(2,j)));

Clippy:"OMG, A NUMBAR! Let me format it for you"
Unsuspecting Student: "Ahhhhh! Damn you paperclip you ruined my paper. A thousand deaths upon you!"

"Game Programming: Without programming you've just got game"

[edited by - cmptrgear on December 13, 2003 4:11:47 PM]

[edited by - cmptrgear on December 13, 2003 4:13:56 PM]

##### Share on other sites
Check out the atoi function, it takes a character and returns an integer. Thus you can do something like:
int iBinary;iBinary = atoi(binary[ i ]);

[edited by - tool_2046 on December 13, 2003 4:11:12 PM]

##### Share on other sites
Or you could just read in an integer to begin with.

"Sneftel is correct, if rather vulgar." --Flarelocke

##### Share on other sites
quote:
Original post by Sneftel
Or you could just read in an integer to begin with.

"Sneftel is correct, if rather vulgar." --Flarelocke

Not that it''s likely to be an issue, but that would lower the maximum value of the number you want to convert.

______________________________________________________________
The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ
MySite
______________________________________________________________

1. 1
Rutin
30
2. 2
3. 3
4. 4
5. 5

• 13
• 14
• 11
• 10
• 14
• ### Forum Statistics

• Total Topics
632961
• Total Posts
3009496
• ### Who's Online (See full list)

There are no registered users currently online

×