The statement
temp = (*(input)) >> (4*(i-1));
could be rewritten as
uint8 x = *(input);
temp = x >> (4 * (i - 1));
or
temp = input[0] >> (4 * (i - 1));
And now you can see that you are actually shifting the same value for 0, 4, 8, 12,... bits to the right. And when shifting value to the right, you are padding 0's from the left side, therefore after 2 iterations of the loop, your temp
variable is always 0.
The result of E1 >> E2 is E1 right-shifted E2 bit positions. If E1 has an unsigned type or if E1 has a signed type and a nonnegative value, the value of the result is the integral part of the quotient of E1 / 2^E2. If E1 has a signed type and a negative value, the resulting value is implementation-defined. - ISO/IEC 9899:TC3, Section 6.5.7: Bitwise shift operators
You need to increment your input
pointer. However in your code, you would need to repeat the code twice for every byte - for lower and upper 4 bits of the uint8
.
This is how I would do it (replaced macro with inline function as pointed out by Olaf in the comment):
/*! \brief Convert nibble (lower 4 bits) to HEX value to avoid using standard libraries.
*/
static inline __attribute__((always_inline, const)) char
NibbleToHex(uint8_t nibble) {
return ((nibble <= 9) ? ('0' + nibble) : ('A' + nibble - 10));
}
static void HexToAscii(const uint8_t *input, char *output, uint8_t size) {
while (size--) {
*(output++) = NibbleToHex((*input) >> 4u);
*(output++) = NibbleToHex((*input) & 0x0Fu);
input++; /*< Move to the next byte. */
}
}
uint8_t ReferenceNumber[8] = {0x30, 0x40, 0x60, 0x50, 0x80, 0x60, 0x75, 0x95};
HexToAscii(ReferenceNumber, output, sizeof(ReferenceNumber) / sizeof(ReferenceNumber[0]));
Note: output
must always the double the size of the input data (assuming that the size
variable equals the length of the input data).