What should the sscanf function count in this case?

 sscanf(gBufRx, "%hu", &x); 

A string of the form xxx or 0xx (decimal) is transmitted. x - unsigned short .

I would check, but the program is on the controller and there is no way to see what is going on there.

It seems that if you transmit a string of the form xxx (for example, 155), then everything is fine, and if the first is 0 (for example, 090), then a large number is read. What is wrong with me?

  • one
    The only guess that comes to mind is that your pointer gBufRx in the case of 0xx looks wrong. - avp

1 answer 1

According to this - scanf Width Specification - unsigned short int should be read. And there is no obvious problem. Unless, in an unknown way, a number starting from zero is interpreted as in the octal system ...

  • well, why in some unknown way. if at the beginning it is zero, then this is the octal number. Everything is clear according to the standard. But how the compiler eats 090 is a mystery to me. - KoVadim
  • I have a leading zero, that reads decimals without it and everything is fine. MinGW gcc.exe (GCC) 3.4.5 (mingw-vista special r3) Windows 7. - avp
  • Unknown, because one thing is numeric literals that the compiler handles (and it distinguishes between 10, 010 and 0x10), and another thing is the f-tion scanf and its derivatives, which are not involved in automatic conversion between different number systems. - gecube
  • The bugs were a bit out of there. Now reads fine. The format is decimal. - Alexey Kotov