I am developing an ARM embedded application. I stuck on a silly problem - I have an array of unsigned 8-bit integers:
uint8_t days [42] = {0};
This is initial with some data - Initial algorithm is very confusing and the irrelevant variable is introduced to the problem, so I will not return it here again. I look at this array in the debugger variable watch, and I'm pretty sure that it is filled with integer values from 0 to 31.
I want to take any element of this array, say fifteen, and convert it to four *
so that it can be displayed on my LCD screen . I write it again using the sprintf
function:
four d [3] = {'0', '0', '0'}; Springf (d, "% d", day [15]);
Just a note: No, I can not use the stdlib
itoa ()
function, because it does not conform to MISRA A standard, which I am obliged to follow
As a result, I get a binary zero value only in my d
buffer any ideas?
For the MISRA-C compliance, you must be sure to Writing a simple decimal integer for string conversion routine is quite basic things ... here is my attempt at an MISRA-C (2004 and 2012) Compatible Version: sprintf () Or anything from stdio.h either you want to avoid any way to avoid sprintf like plague on any embedded system in general.
Note: uint32_t can be to swap out for a uint8_t, But then you need to add all kinds of places, to prevent indirect type propaganda, as required by MISRA, code will actually be ugly, such as:
str [points- I] = (four) (uint8_t) ((uit8_t) (val% 10u) + '0');
< The only wise thing to do again / again is to divide that mistake into several lines:uint8_t ch = (uit8_t) (val% 10u); Ch = (Uint8_t) (ch + '0'); Str [digits-I] = (four) f;
No comments:
Post a Comment