sscanf failure on RCM4200 with DC 10.60

I’ve come across a strange bug in sscanf when reading hex numbers.

Using the following source:

void main(void)
{
unsigned long msgCRC, calcCRC;
sscanf(“1a08754f”, “%lx”, &msgCRC);
printf("#1 msgCRC = 0x%lx
", msgCRC);

sscanf("ba08754f", "%lx", &msgCRC);
printf("#2 msgCRC = 0x%lx

", msgCRC);

sscanf("8a08754f", "%lx", &msgCRC);
printf("#3 msgCRC = 0x%lx

", msgCRC);

sscanf("80000000", "%lx", &msgCRC);
printf("#4 msgCRC = 0x%lx

", msgCRC);

sscanf("70000000", "%lx", &msgCRC);
printf("#5 msgCRC = 0x%lx

", msgCRC);
}

Generates the following output:
#1 msgCRC = 0x1a08754f
#2 msgCRC = 0x7fffffff
#3 msgCRC = 0x7fffffff
#4 msgCRC = 0x7fffffff
#5 msgCRC = 0x70000000

It looks like whenever the highest order bit in a 32 bit number is high, the sscanf function fails to parse it properly.

I think that your problem come from the format used in sscanf and printf functions. You variable msgCRC is unsigned but you write “%lx” as format. Try with “%ulx” in order to precise that your variable is unsigned.