I am working with the C8051F580 MCU.
using my own hardware.
The board is connected to teraterm via UART0
8bits, 115200
I am trying to print a hex number, see very simple code below.
for some reason I always get two extra "zeros" added at the end.
#include <stdio.h>#include <ctype.h>#include <string.h>#include <stdarg.h>#include <float.h>#include <stdlib.h>//#include <math.h> #include <c8051f58x.h>#include "C8051F580_defs.h"#include "compiler_defs.h"#include "PUTCHAR.c"
unsigned char i;
i = 0x12; printf("i = 0x%02Xh", i);
terminal shows:
i = 0x1200h <== two extra zeros, it does not matter if I add the (char) casting before "i" in the print expression.
Any suggestion will be appreciated
Thank you
Fausto Bartra
For performance reasons, C51 deviates a bit from the standard. You just found such a deviation. The problem here is, that your byte variable does not automatically get converted to an int. You have two options:
1) Convert the byte to an int by using a cast: printf("i = 0x%02Xh", (unsigned int)i);
2) Use the C51 specific "b" type-prefix: printf("i = 0x%02bXh", i);
See: https://developer.arm.com/documentation/ka002530/latest/