2

How does endianness affect enumeration values in C++?

Is the size of an enum dependent upon how many enumerations there are and thus some enums are 1 byte, while others are 2 or 4 bytes?

How do I put an enumeration value into network byte order from host byte order?

WilliamKF
  • 41,123
  • 68
  • 193
  • 295

3 Answers3

1

Enums depend on the compiler. They can be an 1, 2, or 4 bytes (see here). They should have the same endianness as the platform they are used on.

To put an enum value into a specific byte order you would need to know what the system you are on is and what the network expect. Then treat as you would an int. See here for help on conversions.

Community
  • 1
  • 1
Steve Rowe
  • 19,411
  • 9
  • 51
  • 82
1

Endianness affects enumerations no more or less than it does other integer types.

The only guarantee on size is that it must be possible for an enum to hold values of int; the compiler is free to choose the actual size based on the defined members (disclaimer: this is certainly the case for C, not 100% sure for C++; I'll check...)

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
0
  1. Same way it affects everything else.
  2. The compiler is free to choose the minimum required space.
  3. htons(), or if you know you have more than 64k values, htonl().
user207421
  • 305,947
  • 44
  • 307
  • 483