title: Correct and portable way to serialize an enum
body: <p>I need to serialize enum values to be sent over the network. The sending part in in C and runs on an 8-bit micro-controller, with no Protocol Buffers or similar library available. The receiving part is in C++ (Linux). I tried following approach:</p>
  
  <pre><code>enum enum_x {
      V1 = 1,
      V2 = 2,
  };
  
  enum enum_y {
      V3 = 1,
      V4 = 2,
      V5 = 3,
  };
  
  enum enum_z {
      V6 = 1,
      V7 = 2,
      V8 = 3,
  };
  
  uint8_t serialize_enums(enum_x x, enum_y y, enum_z z) {
      return x * 100 + y * 10 + z;
  }
  </code></pre>
  
  <p>But it doesn't seem to be valid. It works OK on my Linux box, but the result is incorrect on an 8bit system (the y value seems to be stored as z and z value is missing). I tried to add casts:</p>
  
  <pre><code>return (uint8_t)x * 100 + (uint8_t)y * 10 + (uint8_t)z;
  </code></pre>
  
  <p>but it did not help. What is the correct approach?</p>