When sending data via the network converting byte data can be achieved via several ways:
12345 --> {0 0 48 57}
typedef unsigned char byte;
//1. Bit shift
int32_t shiftedInteger = (int32_t) (inBytes[0] << 24 | inBytes[1] << 16 |
inBytes[2] << 8 | inBytes[3]);
//2. Reinterpret cast
int32_t reinterpretedInteger = *reinterpret_cast<int32_t*>(&inBytes);
//3. Using unions
union{
byte b[4];
int32_t i;
}unionCast;
memcpy(unionCast.b,inBytes,4);
int_32t unionCasted = unctionCast.i;
Which is the preferable way of converting the data (use on an arduino like microporcessor)?
The union and reinterpretCast method face issues of big vs small endians but will come in handy once working with floating point numbers as simple bitshifts will not be enough to convert the data back. How can I swap endians when using reinterpret_cast?