2023-01-23 05:20 PM
Hello all....
Ok....not sure what i am missing here but this fails miserably....
The compiler hates when I try and assign test =
The return function works fine, I have made the return variable static so as not to lose the scope....test is an array so I expect test is the same as me doing &test[0]...What am I missing here?
uint8_t test[6];
for (uint8_t k = 0; k < NO_ADC_CH; k++)
{
test = hex2Ascii(&stream[k]);
test[4] = '\r';
test[5] = '\n'; //new line
HAL_UART_Transmit_IT(&huart2, test, sizeof(test));
}
char*hex2Ascii(uint16_t *val)
{
unsigned char d;
static char buff[4];
buff[0] = (char)((d = (*val >> 12)) > 9 ? ('A' + d - 10) : ('0' + d));
buff[1] = (char)((d = ((*val >> 8) & 0xf)) > 9 ? ('A' + d - 10) : ('0' + d));
buff[2] = (char)((d = ((*val >> 4) & 0xf)) > 9 ? ('A' + d - 10) : ('0' + d));
buff[3] = (char)((d = (*val & 0xf)) > 9 ? ('A' + d - 10) : ('0' + d));
return buff;
}
Solved! Go to Solution.
2023-01-24 02:24 AM
My personal favorite for hex conversion is:
c = "0123456789ABCDEF"[v & 0xf];
Not always the smallest footprint but cute and compact notation, good for old people.
:)
2023-01-23 07:56 PM
Compile commenting one line at a time to find out what is wrong at compile level.
At least, you can't return a function local variable. It's like putting a letter on fire in a mailbox...
I don't use functions returning char *, try passing this as a 2nd function passing parameter. This will avoid your coding issue. Generated code is more efficient if using no more than 2-3 passing parameters, when exceeding, use a pointed structure. Otherwise, check out the generated ASM code to reach this conclusion.
Return values are mostly error codes.
And while at it, personnaly, hex conversion could use a simple lookup tables such as :
const char Quad2Hex[16] = {
'0', '1',..... '9', 'A', 'B', 'C', 'D', 'E', 'F' };
uint8_t byte = 0xC9;
char s[10];
s[0] = Quad2Hex[byte >> 4];
s[1] = Quad2Hex[byte & 0x0F];
Hope it helps.
2023-01-23 09:51 PM
I don’t think it’s a problem with pointers as such, but C strings.
If you want a C string with 4 characters you must allocate (at least) 5. The wanted characters go into locations 0 to 3 and you have to put an end-of-string marker in location 4. This end-of-string marker has the ascii value 0 i.e. ‘\0’ (not to be confused with the character’0’ which has ascii value 48).
2023-01-24 12:31 AM
>>It's like putting a letter on fire in a mailbox...
made my day
2023-01-24 01:48 AM
Any reasonable compiler should give you warnings/errors with the above code which you should understand and not ignore. You may simply use printf and redirect stdout to the UART by re-implementing _write. Alternatively look for (non-standard) itoa function, sprintf, or find and study one of the many hex2ascii implementation on the web.
hth
KnarfB
2023-01-24 02:24 AM
My personal favorite for hex conversion is:
c = "0123456789ABCDEF"[v & 0xf];
Not always the smallest footprint but cute and compact notation, good for old people.
:)
2023-01-24 09:29 AM
What strange syntax! I'm still amazed at the inventiveness of the programmers...
2023-01-24 10:10 AM
Very nice indeed. Intuitive and concise. :clapping_hands:
2023-01-25 04:49 AM
The code above is NOT an example of strange syntax. Strange syntax is this:
c = (v & 0xf)["0123456789ABCDEF"];
2023-01-25 08:38 AM
I can't believe it, it works, I tested it on VisuaC.
But I don't understand why...