get a number from string

hey guys

i have a string comming in over bleutooth

example

p2000e99a63b73

i only need the number behind the 'e' (99) and to be sure i like to serial print it in the monitor

i hope to find a code like

Serial.println(substring( get the number behind e )) <-- but this is not actually working :confused:

Rebelstudios:
hey guys
i have a string comming in over bleutooth
example
p2000e99a63b73
i only need the number behind the 'e' (99) and to be sure i like to serial print it in the monitor
i hope to find a code like
Serial.println(substring( get the number behind e )) <-- but this is not actually working :confused:

try this out

char MyStr[] = "p2000e99a63b73";
Serial.println(atoi(strchr(MyStr,'e') + 1));

atoi converts the string to an integer
strchr finds the position of the letter e
so your number is located in the array position following the e so add 1 to its location
Z

zhomeslice:
strchr finds the position of the letter e

really? :frowning:

No chance this evens compile...

but that is the idea though, look for ‘e’ in the buffer using correctly strchr(), if it’s not the null pointer then get the value from the next char using atoi() or atol()

J-M-L:
really? :frowning:

No chance this evens compile...

but that is the idea though, look for ‘e’ in the buffer using correctly strchr(), if it’s not the null pointer then use atoi() to get the value from the next char using atoi() or atol()

strchr(MyStr,'e')

Your right sorry should have has the character string as first variable. Thanks for the catch.

zhomeslice:

strchr(MyStr,'e')

Your right sorry should have has the character string as first variable. Thanks for the catch.

Well this is still not good Serial.println(atoi(MyStr[strchr(MyStr,'e') + 1]);As strchr() returns a pointer in the string, not the index of the ‘e’ in the array

It’s not cool to modify past posting without documentation then the conversation does not make any sense

J-M-L:
Well this is still not good

Serial.println(atoi(MyStr[strchr(MyStr,'e') + 1]);

As strchr() returns a pointer in the string, not the index of the ‘e’ in the array

It’s not cool to modify past posting without documentation then the conversation does not make any sense

Lol I'm blending 2 programming languages.
Ok

atoi(strchr(MyStr,'e') + 1)

:slight_smile:

Or to be on the relative safe side

char message[] = "p2000e99a63b73";
char *ptr = strchr(message,’e’); //http://www.cplusplus.com/reference/cstring/strchr/
long value = LONG_MIN; // http://www.cplusplus.com/reference/climits/
if (ptr) {
   value = atol(ptr+1); // http://www.cplusplus.com/reference/cstdlib/atol/?kw=atol
   Serial.println(value); // if no number after the ‘e’ value will be 0
} else {
   Serial.println(F(“Error”)); // and value is LONG_MIN
}

For atol() If the converted value would be out of the range of representable values by a long int, it causes undefined behavior. strtol() would be a more robust cross-platform alternative when this is a possibility.

Thanks sofar.. im going to work with those tips and understand more about it..

when solved i'll share