Skip to main content
yabool2001
Associate III
October 29, 2023
Solved

Proper way to use scanf with uint8_t

  • October 29, 2023
  • 11 replies
  • 8610 views

What is the best way to use sscanf with uint8_t?

When I use:

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

I stuck at: 

void HardFault_Handler(void)

 

I found following workaround that works:

 uint16_t x1 ;
 uint8_t x2 ;
 const char* y = "23" ;
 sscanf ( y , "%hu" , &x1 ) ;
 x2 = (uint8_t) x1 ;

but it doesn't look best. So what is the right way of processing uint8_t in above case?

V/r

yabool2001

This topic has been closed for replies.
Best answer by TDK

Then it's not implemented in the library you're using (nano).

You can link to the standard C++ library and it's likely in there.

TDK_1-1698602531295.png

 

11 replies

AScha.3
Super User
October 29, 2023

just guessing:  you search for a 3-char sequence in a 2-char string - what should happen ? 

telling you, oh, you doing wrong, look here... it tries.

make your string some 50 chars long, then search 3-char sequence there... :)

+ if x is defined as int16 , you cannot use a int8 as target.

If you feel a post has answered your question, please click on " Best Answer ".
TDK
October 29, 2023

If it's implemented, it's this:

 

#include <inttypes.h>

...

 sscanf(y, "%" SCNu8, &x);

 

 

"If you feel a post has answered your question, please click ""Accept as Solution""."
yabool2001
Associate III
October 29, 2023

I've implemented it:

#include <stdio.h>
#include <inttypes.h>

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

 

and it doesn't work:

17:11:02 **** Incremental Build of configuration Debug for project Test_uint8_t_in_sscanf ****
make -j8 all 
arm-none-eabi-gcc "../Core/Src/main.c" -mcpu=cortex-m0plus -std=gnu11 -g3 -DDEBUG -DUSE_HAL_DRIVER -DSTM32G071xx -c -I../Core/Inc -I../Drivers/STM32G0xx_HAL_Driver/Inc -I../Drivers/STM32G0xx_HAL_Driver/Inc/Legacy -I../Drivers/CMSIS/Device/ST/STM32G0xx/Include -I../Drivers/CMSIS/Include -O0 -ffunction-sections -fdata-sections -Wall -fstack-usage -fcyclomatic-complexity -MMD -MP -MF"Core/Src/main.d" -MT"Core/Src/main.o" --specs=nano.specs -mfloat-abi=soft -mthumb -o "Core/Src/main.o"
../Core/Src/main.c: In function 'main':
../Core/Src/main.c:96:16: error: 'SCNu8' undeclared (first use in this function)
 96 | sscanf ( y , SCNu8 , &x ) ;
 | ^~~~~
../Core/Src/main.c:26:1: note: 'SCNu8' is defined in header '<inttypes.h>'; did you forget to '#include <inttypes.h>'?
 25 | #include <inttypes.h>
 +++ |+#include <inttypes.h>
 26 | 
../Core/Src/main.c:96:16: note: each undeclared identifier is reported only once for each function it appears in
 96 | sscanf ( y , SCNu8 , &x ) ;
 | ^~~~~
make: *** [Core/Src/subdir.mk:34: Core/Src/main.o] Error 1
"make -j8 all" terminated with exit code 2. Build might be incomplete.

17:11:02 Build Failed. 2 errors, 0 warnings. (took 645ms)

 whole project is available here: Test_uint8_t_in_sscanf (github.com)

 

V/r

yabool2001

TDK
TDKBest answer
October 29, 2023

Then it's not implemented in the library you're using (nano).

You can link to the standard C++ library and it's likely in there.

TDK_1-1698602531295.png

 

"If you feel a post has answered your question, please click ""Accept as Solution""."
Pavel A.
October 29, 2023

This looks like limitation of newlib-nano (no ll and no hh). Your workaround is perfect.

 

Tesla DeLorean
Guru
October 29, 2023

Always inter-staged via an int as this can be problematic on several platforms.

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
yabool2001
Associate III
October 29, 2023

Hi Tesla,

thanks for interacting. As you see I've already proposed similar workaround.

I'm looking for  better and simple solution.