# How to convert a String to a Byte[] ?

Im having trouble converting a String containing 7 numbers into a a byte
Whats happening is the numbers from the String are not translating through to the “real” number value

For instance, String value of 1 becomes 49, 2 becomes 50, 3 becomes 51, etc.

Obviously I want a String with a value of “1” to equal a byte value 1 not 49…

Here is my code

``````String days = "111111"
byte buff[days.length()];
char c;
for(int i=0;i<7;i++){
c = days.charAt(i);
buff[i] = byte(c);
Serial.print(buff[i]);
}
``````

The output for is 49494949494949 instead of 1111111

How do I fix this!?
I know I must be missing a step here somewhere…

An answer plus a simple explanation would be fantastic!

Obviously I want a String with a value of "1" to equal a byte value 1 not 49...

Then, your expectations are unreasonable. The value of '1' IS 49.

What is it you intend to do with the byte array?

Here's a hint, though. '1' - '0' = 1.

...because ASCII value of "1" is not "1" but "49" - you must decrease 48

``````String days = "111111"
``````
``````for(int i=0;i<7;i++)
``````

You have only six '1’s in your String, but no semicolon after it.

Wow such a simple solution! ..just minus 48 from it...perfect.

"What is it you intend to do with the byte array?" It represents the 7 days of the week as either 0 or 1...on or off...think scheduling...

"You have only six '1's in your String, but no semicolon after it." yes, correct. 2 mistakes in my trying. This is just an extract of a much larger sketch (around 135Kb) hand typed Lucky this html form isnt a C++ compiler.. :P

..just minus 48 from it...perfect.

``````decimalVal = charVal - '0';
``````

is much easier to remember.

It represents the 7 days of the week as either 0 or 1...on or off...think scheduling...

Why not think '0' and '1'? What is the benefit of converting the String to a series of 0s and 1s as opposed to '0's and '1's?