First you fix an error in my program. The buffer needs to be declared large enough to hold the output characters plus the terminating zero added to the string.
Then you change the format specifier to accept an unsigned long
UKHeliBob:
First you fix an error in my program. The buffer needs to be declared large enough to hold the output characters plus the terminating zero added to the string.
Then you change the format specifier to accept an unsigned long
Try this
void setup()
{
Serial.begin(115200);
unsigned long n = 1000000;
char buffer[9];
sprintf(buffer, "%05lx", n);
Serial.println(buffer);
sprintf(buffer, "%05lX", n);
Serial.println(buffer);
}
void loop()
{
}
Hi UKHeliBob,
Thanks a lot, It's working.
Please explain me below lines and relation.
unsigned long n = 1000000;
char buffer[9];
sprintf(buffer, "%05lX", n);
Serial.println(buffer);
unsigned long n = 1000000;
char buffer[9]; //declare a character buffer large enough to hold the characters and terminating zero
sprintf(buffer, "%05lX", n); //format the output and put it in the buffer.
//buffer the output goes here
//05 pad with leading zeroes to make the output 5 characters
//l the input is an unsigned long
//X output is to be in uppercase HEX
//n the value to be formatted
//See http://www.cplusplus.com/reference/cstdio/printf/
Serial.println(buffer); //print the contents of the buffer
char buffer[9]; //declare a character buffer large enough to hold the characters and terminating zero
sprintf(buffer, "%05lX", n); //format the output and put it in the buffer.
//buffer the output goes here
//05 pad with leading zeroes to make the output 5 characters
//l the input is an unsigned long
//X output is to be in uppercase HEX
//n the value to be formatted
//See http://www.cplusplus.com/reference/cstdio/printf/
Serial.println(buffer); //print the contents of the buffer