Hey all I am trying to convert a string into a uint8_t with the following code:

String data = "#255101987";
String tmp1 = data.substring(1, 3);

uint8_t first  = (String)tmp1;

I am getting the error of:

cannot convert 'String' to 'uint8_t {aka unsigned char}' in initialization

Any help would be great as I thought adding (String) in front of the varible would solve the issue but it hasn't as you can tell.

  • Do you want the ASCII value of the character or do you want to parse the string contents as a number? – Ignacio Vazquez-Abrams Jun 29 '15 at 6:38
  • I presume from the substring call s/he expects to find 255 in the variable. – Nick Gammon Jun 29 '15 at 8:46
  • @NickGammon is correct. I am looking for the 255 within the string and needing to convert that to the uint8_t. – StealthRT Jun 29 '15 at 17:01
uint8_t first  = atoi (tmp1.substring(1, 3).c_str ());   

Or even:

String data = "#255101987";
uint8_t first  = atoi (data.substring(1, 3).c_str ());   
  • That worked great. Thanks for the help, Nick! – StealthRT Jun 29 '15 at 22:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.