Converting bytes to ints

Im not very experienced with the data types so Im struggling to get my head around this but I have found a solution, I just feel like its not a very good one.

Im using an adafruit qt py esp32-C3 to receive a 'payload' from MQTT in the type of byte*

Its 3 characters long, the first will be used for a true/false state so 1/0 is acceptable here. The next two characters I want to combine together for a single two digit integer. The payload will only ever be 3 numbers in length so I dont need to account for any more though I know it would be good practice, but in my case it wont matter.

Im converting the values like so

mode = payload[0] - '0';
setpoint = (payload[1] - '0') * 10 + payload[2] - '0';

It works, but if theres a better way to do it Id like to know.

Thanks

You are converting characters to int.
You can use atoi()
There is another function that does the same and is more portable.... but I forgot its name.

There are many ways to do it, not sure why one would be better than the next.

I like to program directly seeing what I am doing, so I think your solution is fine. You sometimes see the same kind logic making an integer by collecting characters, each new character's numerical value added to 10 times the current accumulated integer.

This just in

You can use atoi()

from @build_1971 whilst typing is another way. Here, however, atoi() wants to work with a null-terminated string, so you'd need to have an extra character after the two digits with a '\0' in it to mark the end of the integer. Which is not a big deal.

Certainly for 3 or 4 or more characters it would be better to use the function.

a7

who is creating the payload? if you have control over it and need to generate a bool and a number between 0 and 99, all this could fit in 1 byte. the MSb could be the bool and then you can represent 0..127 on the 7 other bits âžś your payload then is only one byte.

and you extract the info with a simple code

mode = bitRead(*payload, 7);  // https://www.arduino.cc/reference/en/language/functions/bits-and-bytes/bitread/
setPoint = *payload & 0x7F;

strtoul or it's signed counter part.

strtoul() I presume..

char aNumeral = '7';
Serial.print(aNumeral-48);

Hum…

Why not just

char aNumeral = '7';
Serial.print(aNumeral);

Or

char aNumeral = '7';
Serial.write(aNumeral);

Because mine is right. Try adding yours to itself.

If you just print there is no value added…

I would understand better

char aCharDigit = '7';
int  theDigit = aCharDigit - '0';

If you want to do maths with that digit

Mine does math. Try yours and divide by '0'

The question was "how to do atoi() without atoi()" ... QED.

Your code only prints out 7. Mine too.

If you just print the char there is no need to convert it to an integer first.

That was my point.

If you do maths, then sure thing, but give an example doing that (and it’s better for readability to use '0' than 48)

That’s what OP is doing in his first post

And the question was

No, you are wrong again. Mine prints the VALUE of seven. Yours is wrong and prints 7. Not what anyone was seeking. Quit running around the forum looking only to start petty fights. So write your pages-long-soliloquy back to me to prove how "right" you are, and flag me for moderation and suspension again, just like you did last year when you chased me all over the forum to get me booted for responding in French, because you insisted the French pages were only for the French because I couldn't possibly understand... typical.

I don’t get your rant

I was commenting on the example you gave without much comment. The byte going out is the ASCII code for '7' whether you do that conversion to an int or not. If you convert to an int before calling print, then print has to convert it the other way around back to a char. There is limited value to that and That was my point.

To the original question - OP was asking a different way for getting the numeric value than subtracting the '0' ASCII code.

If i reported you and mods agreed there was an issue then that’s what it was. I don’t remember it.

Enough digressions for me here.

thanks, seems like my method isnt completely terrible so I will leave it as is.

As for one of the questions, Im created the payload from elsewhere so I know it will always be three digits and what they will be limited to which is why I wasnt too worried about capturing everything else for error checking etc.

my point in post #4 was to suggest to bundle the mode and setpoint into 1 byte this way you would not have to worry about decoding and you'll send 3x less bytes in the payload

OK, yes I see what you mean there. Thanks for the suggestion.