I need to communicate with an 8 bit device on which there is no nthol function. It is also impossible to use protocol buffers library.
Will following C function produce the same output string no matter on what architecture it is run (different endianness, different number of bits):
void encode(uint32_t input, unsigned char* buf, int buf_len) {
assert(sizeof(uint32_t) <= buf_len);
buf[0] = input >> 24 & 0xFF;
buf[1] = input >> 16 & 0xFF;
buf[2] = input >> 8 & 0xFF;
buf[3] = input & 0xFF;
}
Will following function correctly read such encoded string and produce the same result on all architectures:
void decode(const unsigned char* buf, int buf_len, uint32_t* output) {
assert(sizeof(uint32_t) <= buf_len);
*output = buf[0];
*output <<= 8;
*output |= buf[1];
*output <<= 8;
*output |= buf[2];
*output <<= 8;
*output |= buf[3];
}
Is it possible that on an 8 bit processor 32 bit integers won't be handled correctly for some reason?
Yes, that looks fine. A couple of comments:
uint8_t
instead of assuming unsigned char
is 8-bit, when the code relies on that fact.output
all the time.decode()
function a bit cleaner.-
uint32_t decode(const uint8_t *buf, size_t buf_len) {
uint32_t out = 0;
assert(sizeof out <= buf_len);
out |= buf[0];
out <<= 8;
out |= buf[1];
out <<= 8;
out |= buf[2];
out <<= 8;
out |= buf[3];
return out;
}