Skip to main content
yabool2001
Associate III
November 4, 2023
Solved

SCNu8 formatting

  • November 4, 2023
  • 3 replies
  • 1980 views

Hi,

I was expecting to get x = 23 after following code:

 uint8_t x ;
 char* y = "23" ;
 sscanf ( y , SCNu8 , &x ) ;

but I got x = 32.

debug.jpg

 

This topic has been closed for replies.
Best answer by Pavel A.

From comment in inttypes.h:

NOTICE: scanning 8-bit types requires use of the hh specifier
* which is only supported on newlib platforms that
* are built with C99 I/O format support enabled. If the flag in
* newlib.h hasn't been set during configuration to indicate this, the 8-bit
* scanning format macros are disabled here as they result in undefined
* behaviour which can include memory overwrite.

Build config of newlib is in ... tools/arm-none-eabi/include/newlib-nano/newlib.h and there _WANT_IO_C99_FORMATS is not enabled. Hope this answers your question, see also here. 

 

3 replies

Tesla DeLorean
Guru
November 4, 2023

What does sscanf() return?

What is SCNu8 ?

uint8_t x = 0;
char* y = "23" ;
int z = sscanf ( y , SCNu8 , &x ) ;

 

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
Tesla DeLorean
Guru
November 4, 2023

Would suggest inter-staging via an unsigned int.

 

uint8_t sscanf_u8(char *string)
{
 unsigned int x = 0;
 return(sscanf(string, "%u", &x) == 1 ? (uint8_t)x : 0 );
}

 

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
Pavel A.
Pavel A.Best answer
November 4, 2023

From comment in inttypes.h:

NOTICE: scanning 8-bit types requires use of the hh specifier
* which is only supported on newlib platforms that
* are built with C99 I/O format support enabled. If the flag in
* newlib.h hasn't been set during configuration to indicate this, the 8-bit
* scanning format macros are disabled here as they result in undefined
* behaviour which can include memory overwrite.

Build config of newlib is in ... tools/arm-none-eabi/include/newlib-nano/newlib.h and there _WANT_IO_C99_FORMATS is not enabled. Hope this answers your question, see also here.