You can work on the value of your int
using masking and bit-shifting, something like:
int foo = 42
unsigned char lsb = (unsigned)foo & 0xff
unsigned char msb = (unsigned)foo >> 8
This has the advantage that you're independent of the layout of your int
in memory. For reconstruction, do something like:
int rec = (int)(((unsigned)msb << 8) | lsb );
Note casting msb
to unsigned
here is necessary, as otherwise, it would be promoted to int
(int
can represent all values of an unsigned char
), which could overflow when shifting by 8 places. As you already stated your int
has "two bytes", this would be very likely in your case.
The final cast to int
is implementation-defined as well, but will work on your "typical" platform with 16bit int
in 2's complement, if the compiler doesn't do something "strange". By checking first whether the unsigned
is too large for an int
(because the original int
was negative), you could avoid this, e.g.
unsigned tmp = ((unsigned)msb << 8 ) | lsb;
int rec;
if (tmp > INT_MAX)
{
tmp = ~tmp + 1;
if (tmp > INT_MAX)
{
rec = INT_MIN;
}
else
{
rec = tmp;
rec = -rec;
}
}
else
{
rec = tmp;
}
The 2's complement is fine here, because the rules for converting a negative int
to unsigned
are explicitly stated in the C standard.
You can use the representation in memory, like:
int foo = 42;
unsigned char *rep = (unsigned char *)&foo;
unsigned char first = rep[0];
unsigned char second = rep[1];
But beware whether first
will be the MSB or LSB depends on the endianness used on your machine. Also, if your int
contains padding bits (extremely unlikely in practice, but allowed by the C standard), you will read them as well. For reconstruction, do something like:
int rec;
unsigned char *recrep = (unsigned char *)&rec;
recrep[0] = first;
recrep[1] = second;
htons()
和ntohs()
。你可以使用htons()
将一个两字节的int
值转换为网络字节序,然后在接收时使用ntohs()
将其转换回接收主机的主机字节序。 - Andrew Henle>>
和& 0xff
的答案,根据我的答案的建议仔细测试,那么您应该没问题了。 - Steve Summit