AwfulMint wrote:
Code: Select all
const uint16_t digits[] = {
u'0', u'1', u'2', u'3', u'4', u'5', u'6', u'7', u'8', u'9', u'a', u'b', u'c', u'd', u'e', u'f'
};
You
probably don't want
uint16_t for your implementation. In this case I was working with UEFI, which uses wide characters. Unless you're also in UEFI-land, you most likely want just
char. (Which also means you don't need the
u prefix on your character literals.
AwfulMint wrote:
Code: Select all
uint8_t c[] = {'O', 'i'};
uint32_t *p = (uint32_t*) c;
printHexDigit(p[0]);
printHexDigit(p[1]);
Take a step back and think about what you're asking the compiler for with the data types here. Remember than in C, a pointer and an array are not very different. So you're telling the compiler to allocate two bytes on the stack for 'Oi', and
c points to those two bytes. You then make
p point to the same address, and then ask it to read 4 bytes starting at
p as a
uint32_t, and then the next 4 bytes after that as a
uint32_t, when you only allocated 2 bytes.
What you probably want here is the address of each
uint8_t in array
c. And you want to convert the address (not the value) to a
uint32_t. So instead of casting it as pointer
(uint32_t*) you just want to treat it as a value
(uint32_t).
You might want to refresh yourself on pointers and datatypes (
here's a short tutorial). As for why you see "iO" in the output, it's because you're treating that memory as a 32-bit number on a
little-endian computer - what used to be known as the "NUXI" problem on old UNIX machines.