Domanda

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?

È stato utile?

Soluzione

Yes, that looks fine. A couple of comments:

  1. You should use uint8_t instead of assuming unsigned char is 8-bit, when the code relies on that fact.
  2. It would be a bit cleaner (imo) to do the receive using a local variable, so you don't have to shift through the pointer to output all the time.
  3. You can probably just return the decoded value, making the 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;
 }

Altri suggerimenti

Yes it will work. The C shift operator will do the right thing no matter which endianess or word-size the machine has.

Just remember to do the exact same thing in all programs communicating with this protocol.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top