another simple PSTR question: unsigned long format

Hi - still working through stuff with PSTR. I've got it to work with unsigned ints but I would like to use unsigned longs.

(background: I am using an array of type 'char' to store input from Serial.read(). When I reach the control char I want to pull the numeric representation out of the buffer of chars and use that as a number. It is possible that my data > that which an unsigned int can hold).

So right now I have this to generate the number:

X_BUFFER[Xindex++] = 0;
unsigned int x;
sscanf_P(X_BUFFER, PSTR("%u"), &x);

I searched around but can't find the format to indicate that an unsigned long should be used.

Oh - while I am here, I was trying to pass a length param (Xindex) to only read the num chars that had been stored from the most recent input and so be able to reuse the buffer without initializing it again. Couldn't figure out how to do that - but writing a 0 into the buffer with this line

X_BUFFER[Xindex++] = 0;

seems to work - so I get back a number of the correct length and without garbage chars added from past data sent. No idea why this works.

thanks for all the help!
--Roy

Oh - while I am here, I was trying to pass a length param (Xindex) to only read the num chars that had been stored from the most recent input and so be able to reuse the buffer without initializing it again. Couldn't figure out how to do that - but writing a 0 into the buffer with this line

That's because sscanf expects a 'C' string, which is an array of chars, where the last character is a zero, sometimes represented '\0'.
The compiler automatically includes such a character for strings declared in the program, (e.g. "%u" actually consists of three chars '%' 'u' and '\0'), but for input strings, you got to put your own on (unless handled by libraries or interface functions).

Ah, got it. Thanks. Was thinking it had something to do with PSTR. Doh!

This peek n' poke method I seem to be using in learning bits of C is probably not the way to go about it... :wink:

I can't use PSTR with LCD function, Don't Know Why?

#include <avr/pgmspace.h>
(............)
lcd.printIn(PSTR("Arduino")); //NOT RUN
lcd.printIn("Arduino"); //RUN

prototype:

uint8_t LCD4Bit_mod::printIn(char *msg);

You must use pgm_read_byte() in lcd constant string function?