Jump to content
  • Advertisement
Sign in to follow this  
phil67rpg

binary string to decimal

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am attempting to convert a string binary to a decimal value.
[font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]#include[/font][/font][/font][font="Consolas"][font="Consolas"] [/font][/font][font="Consolas"][color="#a31515"][font="Consolas"][color="#a31515"][font="Consolas"][color="#a31515"]<iostream>

[/font][/font][/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]#include[/font][/font][/font][font="Consolas"][font="Consolas"] [/font][/font][font="Consolas"][color="#a31515"][font="Consolas"][color="#a31515"][font="Consolas"][color="#a31515"]<string>

[/font][/font][/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]#include[/font][/font][/font][font="Consolas"][font="Consolas"] [/font][/font][font="Consolas"][color="#a31515"][font="Consolas"][color="#a31515"][font="Consolas"][color="#a31515"]<math.h>

[/font][/font][/font][font="Consolas"][font="Consolas"][/font][/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]using[/font][/font][/font][font="Consolas"][font="Consolas"] [/font][/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]namespace[/font][/font][/font][font="Consolas"][font="Consolas"] std;

[/font][/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]int[/font][/font][/font][font="Consolas"][font="Consolas"] main()

{

[/font][/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]int[/font][/font][/font][font="Consolas"][font="Consolas"] test,dec=0;

string bin;

cout << [/font][/font][font="Consolas"][color="#a31515"][font="Consolas"][color="#a31515"][font="Consolas"][color="#a31515"]"Enter a binary string: "[/font][/font][/font][font="Consolas"][font="Consolas"];

cin >> bin;

[/font][/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]for[/font][/font][/font][font="Consolas"][font="Consolas"]([/font][/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]int[/font][/font][/font][font="Consolas"][font="Consolas"] i=0; i<bin.size(); i++)

{

cout << bin;

}



[/font][/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]for[/font][/font][/font][font="Consolas"][font="Consolas"]([/font][/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]int[/font][/font][/font][font="Consolas"][font="Consolas"] j=0;j<bin.size();j++)

{

dec += bin[j]*2^j;

}

cout << [/font][/font][font="Consolas"][color="#a31515"][font="Consolas"][color="#a31515"][font="Consolas"][color="#a31515"]" = "[/font][/font][/font][font="Consolas"][font="Consolas"] << dec << [/font][/font][font="Consolas"][color="#a31515"][font="Consolas"][color="#a31515"][font="Consolas"][color="#a31515"]" in decimal"[/font][/font][/font][font="Consolas"][font="Consolas"];

cout << endl;

cin >> test;



[/font][/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]return[/font][/font][/font][font="Consolas"][font="Consolas"] 0;

}

thanks for your help in advance.

[/font][/font]

Share this post


Link to post
Share on other sites
Advertisement
[font="Consolas"][font="Consolas"]
thanks for your help in advance.[/font][/font]

Help with what? You just said "I am attempting to convert a string binary to a decimal value" but never said anything was wrong or what you needed help with or what your question is.

Share this post


Link to post
Share on other sites
well I think there is a problem with this line of code.
[font="Consolas"]
[font="Consolas"][/font]dec += bin[j]*2^j;[/font]

Share this post


Link to post
Share on other sites
The ^ operator is not a power operator, like maths. It is the bitwise exclusive OR operator.

The value of a numberic character is not the same as the number it represents. That is, '0' != 0, '1' != 1 and so forth. In fact, the character with a value of 0 is quite special, it is used as the terminator for C strings and string literals.

A common technique to convert characters to numerics is to take advantage that in most common character encodings the numbers are contiguous and ascending. Thus, one can write:

int toNumber(char c)
{
if(c < '0' || c > '9')
{
// Errors are represented as negative result
return -1;
}
return c - '0';
}

Share this post


Link to post
Share on other sites
I am attempting to convert a string binary to a decimal value.
[font="Consolas"][font="Consolas"][color="#0000ff"]...[/font][/font]
[font="Consolas"][font="Consolas"]thanks for your help in advance.[/font][/font]


Help with what? I don't see a question. I'm pretty sure I know what you're asking, but still, no question.

Did you try searching for this? It's not exactly a hard to find answer. Google returns 10,500,000 results for binary string to decimal

http://www.cplusplus.../general/13492/


Sixth post down.

EDIT:
Ninja'd by several people!

Share this post


Link to post
Share on other sites
You have to take into account that your math is correct, and that of course that ^ symbol as stated is not the one you want but also:

00000010
^ = 2^8*0 ( bin[0] not bin[8] though as you have it). because you have 2^j*0 where j = 0, the first character in the string.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!