Reconstructing a byte

Is there a better way to reconstruct a byte than the “for loop” below, when the numbers are coming through as characters?

byte val=0;

for(int i=0; i<len; i++){

   val=(val*10)+(buf[i]-48);

}

So a value of 255, will be received with the numbers in sequence ‘2’, ‘5’ and ‘5’
Where ‘2’ = 50, ‘5’=53, and ‘5’=53

buf[0]=50
buf[1]=53
buf[2]=53

I could receive any number between 0-255. and len determines the number characters received.

What do you mean by better? Faster? Less memory used? Or just less code to type?

byte val = (buf[0] - '0') * 100 + (buf[1] - '0') * 10 + (buf[2] - '0');

This doesn’t check the input, so val can easily overflow (but then again, so can your for loop).

What do you mean by "better"
What do you mean by "coming through" ?

If the characters are being received from a serial link then you could avoid the use of an intermediate array and add them to the total as they become available

If you must use an intermediate array then if you terminate it with a zero it will be a string and can be converted into an int using the atioi() function.

Haha - I often get frustrated when people ask questions like “is there a better way”, when “better” means different things to different people, and here I am doing it myself… oh well… I guess we all have our moments.

Ok - I will try to be a bit more specific.

I am receiving information via an MQTT callback function using the PubSubClient.h library.
Here is a snippet of that callback function from one of the examples of that library.

void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  for (int i=0;i<length;i++) {
    Serial.print((char)payload[i]);
  }
  Serial.println();
}

Whenever a slider widget is used on a Web-based dashboard, it will trigger the callback function on my microcontroller. I will then have access to the topic, payload, and length variables.

The topic allows me to identify the specific widget being used on the web-based dashboard. The payload will allow me to retrieve the value of the widget (0-255). And the length will tell me how many characters the payload array is storing.

So I have figured out that when the slider widget changes to a value of 255, the payload array will contain the characters ‘2’, ‘5’, and ‘5’.

Whereby,

payload[0] = 50
payload[1] = 53
payload[2] = 53

So what I guess I am trying to ask, is there a way to get the value of 255 from the payload array more efficiently than using the for-loop as described in my original question.

I just wanted to make sure that I wasn’t missing something obvious. Like if you wanted to reconstruct the payload array into a byte which equals 255, how would you do it ?

Keep in mind that the slider value transmitted can be any value from 0 to 255.

void setup()
{
  Serial.begin(115200);
  char payload[4];
  payload[0] = 50;
  payload[1] = 53;
  payload[2] = 53;
  payload[3] = '\0';
  int theInt = atoi(payload);
  Serial.println(theInt);
  Serial.println(theInt * 11); //to prove that it is actually an int
  payload[0] = 49;  //prove it was not a fluke
  payload[1] = 50;
  payload[2] = 51;
  theInt = atoi(payload);
  Serial.println(theInt);
  Serial.println(theInt * 11); //to prove that it is actually an int
}

void loop()
{
}

Thanks - I will give that a go.

Note the need for the terminating '\0'

It is possible that it is already there but if you need to add it yourself then make sure that it is within the bounds of the array