1

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.

asked Jun 29, 2015 at 5:31
3
  • Do you want the ASCII value of the character or do you want to parse the string contents as a number? Commented Jun 29, 2015 at 6:38
  • I presume from the substring call s/he expects to find 255 in the variable. Commented Jun 29, 2015 at 8:46
  • @NickGammon is correct. I am looking for the 255 within the string and needing to convert that to the uint8_t. Commented Jun 29, 2015 at 17:01

1 Answer 1

3
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 ()); 
answered Jun 29, 2015 at 6:12
1
  • That worked great. Thanks for the help, Nick! Commented Jun 29, 2015 at 22:52

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.