cancel
Showing results for 
Search instead for 
Did you mean: 

SCNu8 formatting

yabool2001
Associate III

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

 

1 ACCEPTED SOLUTION

Accepted Solutions
Pavel A.
Evangelist III

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. 

 

View solution in original post

3 REPLIES 3

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 Up vote any posts that you find helpful, it shows what's working..
Pavel A.
Evangelist III

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. 

 

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 Up vote any posts that you find helpful, it shows what's working..