2023-10-29 06:58 AM
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
Solved! Go to Solution.
2023-10-29 11:02 AM
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.
2023-10-29 07:08 AM - edited 2023-10-29 07:15 AM
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.
2023-10-29 07:16 AM - edited 2023-10-29 11:01 AM
If it's implemented, it's this:
#include <inttypes.h>
...
sscanf(y, "%" SCNu8, &x);
2023-10-29 07:59 AM
This looks like limitation of newlib-nano (no ll and no hh). Your workaround is perfect.
2023-10-29 08:14 AM
Always inter-staged via an int as this can be problematic on several platforms.
2023-10-29 09:20 AM
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
2023-10-29 09:34 AM
Hi Tesla,
thanks for interacting. As you see I've already proposed similar workaround.
I'm looking for better and simple solution.
2023-10-29 11:02 AM
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.
2023-10-29 12:56 PM
Thanks a lot.
2023-10-31 10:06 AM
Hi,
I was expecting to get x = 23 after following code:
uint8_t x ;
char* y = "23" ;
sscanf ( y , SCNu8 , &x ) ;
but I got always x = 32 regardless of the value in string.