# binary string to decimal wont work

Hey,
I am working on a project to send data via fiber, but I hit a little problem when trying to decode the binary string.

``````String binary;
int decimalnumber;
// the setup routine runs once when you press reset:
void setup() {
// initialize the digital pin as an output.
Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
binary = "00110001";
Serial.println(Decimal());
Serial.println(decimalnumber);

}
int Decimal(){

int decimalnumber = 0;
int twopotence = 1;

for (int i = binary.length(); i > 0; i--) {
if (binary.charAt(i) == 1) {
decimalnumber = decimalnumber + twopotence;
}
twopotence = twopotence * 2;
}
return decimalnumber;
}
``````

Thats what I tried. There is no error message but it just keeps spitting out 0s and no decimal numbers that I just converted. Does anybody know what the problem is?
And does anybody know how to get the decimal to a char

TheUnnamedCircuit.ino (1017 Bytes)

You never zero decimalnumber, apart from its initial value.
Why do you write in italics?

`````` if (binary == 1)
``````

Your string consists of ASCII characters so you have to test individual characters in the string not the whole string. Also ASCII for a one is 0x31 and ASCII for a zero is 0x30.

How to use this forum

AWOL:
You never zero decimalnumber, apart from its initial value.
Why do you write in italics?

Hey, I am new here and just made a mistake, it was nothing I wanted to do:)

meinkrafter2000:
Hey, I am new here and just made a mistake, it was nothing I wanted to do:)

...which is why simple guidelines for newbies on how to post are displayed in just about every section of the forum.

Grumpy_Mike:

`````` if (binary == 1)
``````

Your string consists of ASCII characters so you have to test individual characters in the string not the whole string. Also ASCII for a one is 0x31 and ASCII for a zero is 0x30.

How to use this forum

Hey I just modyfied my post and hope it is a little better now. I did test the individual characters, the forum just read it as a sign for italic. I hope it is better now. How can I get it working? Any ideas?

`if (binary.charAt(i) == '1')`etc

AWOL:
`if (binary.charAt(i) == '1')`etc

Thank you:)

After just another click it worked.
The finished code is

``````String binary;
int decimalnumber;
// the setup routine runs once when you press reset:
void setup() {
// initialize the digital pin as an output.
Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
decimalnumber = 0;
binary = "00110010";
Serial.println(Decimal());
Serial.println(decimalnumber);

}
int Decimal(){

int decimalzahl = 0;
int twopotence = 1;

for (int i = binary.length()-1; i > 1; i--) {
if (binary[i] == '1') {
decimalnumber = decimalnumber + twopotence;
}
twopotence = twopotence * 2;
}
return decimalnumber;
}
``````

Why Strings?

Why reinvent the wheel? strtoul()

``````char binary[] = "110001110011100110010";
void setup() {
Serial.begin(9600);
Serial.print(F("binary  = 0b"));
Serial.println(binary);
Serial.print(F("decimal = "));
Serial.println(strtoul(binary, NULL, 2));
}
void loop() {}
``````
``````binary  = 0b110001110011100110010
decimal = 1632050
``````

Whandall:
Why Strings?

Why reinvent the wheel? strtoul()

``````char binary[] = "110001110011100110010";
``````

void setup() {
Serial.begin(9600);
Serial.print(F("binary  = 0b"));
Serial.println(binary);
Serial.print(F("decimal = "));
Serial.println(strtoul(binary, NULL, 2));
}
void loop() {}

``````

``````

binary  = 0b110001110011100110010
decimal = 1632050

Because I just didnt know how a wheel looked:D