In a (real time) system, computer 1 (big endian)
gets an integer
data from from computer 2
(which is little endian
). Given the fact that we do not know the size of int
, I check it using a sizeof()
switch
statement and use the __builtin_bswapX
method accordingly as follows (assume that this builtin method is usable).
...
int data;
getData(&data); // not the actual function call. just represents what data is.
...
switch (sizeof(int)) {
case 2:
intVal = __builtin_bswap16(data);
break;
case 4:
intVal = __builtin_bswap32(data);
break;
case 8:
intVal = __builtin_bswap64(data);
break;
default:
break;
}
...
is this a legitimate way of swapping the bytes for an integer
data? Or is this switch-case statement totally unnecessary?
Update: I do not have access to the internals of getData()
method, which communicates with the other computer and gets the data. It then just returns an integer data which needs to be byte-swapped.
Update 2: I realize that I caused some confusion. The two computers have the same int size but we do not know that size. I hope it makes sense now.