3

Convert text array/buffer to integer and string.

hi! i'm sending text to an arduino and the output of that library is a uint8_t array.

Atm the text send is a number between "0" and "1023"

void something(uint8_t * text){
 analogWrite(13,text);// does not work
}
/*
toInt() also not
int() not
atoi() not
invalid conversion from 'uint8_t* {aka unasigned char*}' to 'const char*'
*/

i need to convert it to an integer or better to a uint16_t.

As i plan to send custom commands in near future it would also be good to know how to convert that array to a string, that i then split/manipulate/parse.. whatever.

i looked in various posts but i couldn't get it to work on that simple example above. i'm new to c++

if it's needed the length is also aviable inside the function.

asked Nov 15, 2015 at 3:43

3 Answers 3

4

Your problem is that you are storing your text in an unsigned pointer (uint8_t *)instead of a signed pointer (char *).

If you change all your text types to char * then you can directly use atoi(). If you have a lot to change, though, you can just cast the unsigned pointer into a signed one (and make it const at the same time):

analogWrite(13, atoi((const char *)text));
answered Nov 15, 2015 at 11:36
4
  • its from a websocket lib... i would need to rewriter the whole lib...anyway this worked perfectly Commented Nov 15, 2015 at 13:20
  • btw i will update this analogwrite at 60Hz . is "cast" slow/are there faster ways to convert the buffer to a string or int? Commented Nov 15, 2015 at 13:35
  • @cocco Cast makes no difference at runtime, only compile time. There are faster ways to convert than atoi, but they aren't as safe - i.e., they don't do any bounds checking. Commented Nov 15, 2015 at 13:36
  • it works but i have a bigger problem now. it crashes really fast. i can't send data faster than 2 times per second. maybe you know more about this.arduino.stackexchange.com/questions/17811/esp8266-websockets Commented Nov 16, 2015 at 10:25
0

analogWrite is a function to set a PWM duty cycle value to a pin on the micro

Usually to control a motor speed or LED brightness or similar.

Unless you plan to use the char value to set the PWM value??

A bit more context would help with your problem.

answered Nov 15, 2015 at 13:25
2
  • i control a led with websockets. now it works. @Majenko answered it correctly Commented Nov 15, 2015 at 13:28
  • This isn't really an answer. Commented Oct 31, 2016 at 23:03
-1

I've converted first four bytes to string, and then to int :

void something(uint8_t * payload){
String str="";
char c;
for(int i=0;i<4;i++)
 if (isDigit(c=(char)(*(payload+i))))
 str += c;
int Val =str.toInt();
Val=Val>1023?1023:Val;
analogWrite(14,Val);
}
answered Jul 18, 2016 at 21:49
1
  • This is a very inefficient approach. Commented Oct 31, 2016 at 23:03

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.