Question
Write a function in C that takes a 64-bit number and converts it to an array of 8-bit numbers.
Answer
The lowest eight bytes can be simply copied in to the destination. A cast should be used to avoid a warning.
The 64-bit value can be shifted 8-bits for each loop to obtain the next value.
#include <inttypes.h>
void byte64(uint64_t x, uint8_t *y) {
int i;
for (i = 0; i < 8; i++) {
y[i] = (uint8_t) x;
x = x >> 8;
}
}
Background
This question was given during an interview on 1/17/2014 for a firmware test engineer position.