andreahmed - 9 months ago 62

C Question

I would like someone to explain to me this weird function that is found in the code base in my work.

`bool uint32tox(const UINT32 input, UINT8 *str)`

{

UINT32 val = input;

INT16 i;

UINT16 j = 0;

if (str == NULL) return(false);

for (i = 28; i >= 0; i -= 4) {

UINT8 sval = (val >> i) & 0x0F;

str[j++] = (sval < 10u) ? sval + '0' : sval - 10 + 'A';

}

str[j] = '\0';

return(true);

}

why anding with 0x0F, why i starts with 28.

Answer

I took the liberty to comment the code a bit

```
/*
Convert an unsigned 32-bit (assuming UINT32 to mean uint32_t or similar) large
integer to a hexadecimal representation
*/
bool uint32tox(const UINT32 input, UINT8 *str)
{
UINT32 val = input;
INT16 i;
UINT16 j = 0;
if (str == NULL) return(false);
for (i = 28; i >= 0; i -= 4) {
// Shift input by i bits to the right and snip of the rightmost 4 bits
/*
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
with i = 28 sval contains the leftmost four bits 28 - 30
with i = 24 sval contains bits 24-27
with i = 20 sval contains bits 20-23
with i = 16 sval contains bits 16-19
with i = 12 sval contains bits 12-15
with i = 8 sval contains bits 8-11
with i = 4 sval contains bits 4- 7
with i = 4 sval contains bits 0- 3
*/
UINT8 sval = (val >> i) & 0x0F;
// If sval is smaller than ten we can use a base ten digit
// that gets constructed by adding the numerical value of ASCII '0'
// to sval (the digits 0-9 are guaranteed to be in order without gaps).
// If sval is bigger we need a hexdigit, one of A, B, C, D, E, F.
// These are normally consecutive at least in ASCII, so you can handle it
// like the other branch and just add the numerical value of 'A' instead
str[j++] = (sval < 10u) ? sval + '0' : sval - 10 + 'A';
}
// terminate the UINT8 array such that it can be used as a C-string
str[j] = '\0';
return(true);
}
```

Source (Stackoverflow)