Converting a string to byte

Hi,
The issue I am experiencing is when I try to store a string into the EEPROM. It will take only byte values and despite the string which I have being numerically less than 255, I can not store it in the EEPROM like I want to.
Any suggestions on how I can do that?
I want to be be able to use the value later. I could have tried the atoi() command and used the values if I wasn't storing them in the EEPROM.

Can I convert the string to int using atoi and the cast that to a byte?

let me get that straight:

you have a variable that holds a string like "129" (an ASCII representation, 3 characters and a terminating null character)
you want to save that into EEPROM like a number 129
but you don't use atoi() and expect miracles to happen?

did I get that right?

abhir24:
Can I convert the string to int using atoi and the cast that to a byte?

well you can directly store it into a byte, the compiler will only keep the LSB, which is what you want if you know your number is less than 255

byte b = atoi("129");
1 Like

Please post the code so that we can see how the string representing a number less that 255 is created.

Is it really a string (zero terminated array of chars) or a String (an object created using teh String library) ?

J-M-L:
let me get that straight:

you have a variable that holds a string like "129" (an ASCII representation, 3 characters and a terminating null character)
you want to save that into EEPROM like a number 129
but you don't use atoi() and expect miracles to happen?

my bad, I should have used atoi() and checked. I assumed that it would convert it to a 16 bit int and that couldnt have been stored into the EEPROM. It works perfectly fine now! Thank you!

UKHeliBob:
Please post the code so that we can see how the string representing a number less that 255 is created.

Is it really a string (zero terminated array of chars) or a String (an object created using teh String library) ?

It is a zero terminated array of chars and using the atoi command helped! Thank you!

EEPROM.update(addrA, atoi(finalData));

Great ! :slight_smile: