On May 2, 7:52 pm, Ben Bacarisse <ben.use...@bsb .me.ukwrote:
Would memcpy and htons & co be a correct approach? My problem is that
it's not only short ints that I have to accomodate, but also the
occasional long; I'd like to have a uniform handling of these
operations.
Also, I'm not sure if the way I found to "deserializ e" these values is
safe and recommended:
x = htons(x);
memcpy(&buffer, &x, sizeof x);
/* Later, at the other end: */
x = ntohs(*((unsign ed short *) buffer));
I only need unsigned values, so unsigned {short, long} should be
enough. And one final question: would there be any reason to using
uint16_t and uint32_t instead of unsigned short and unsigned long?
Thanks and sorry for the question avalanche,
Vlad
memcpy(&char_bu f, &int_var, sizeof int_var);
[snip]
The shifting method (used on unsigned types) can give you
a portable solution.
[snip]
The shifting method (used on unsigned types) can give you
a portable solution.
it's not only short ints that I have to accomodate, but also the
occasional long; I'd like to have a uniform handling of these
operations.
Also, I'm not sure if the way I found to "deserializ e" these values is
safe and recommended:
x = htons(x);
memcpy(&buffer, &x, sizeof x);
/* Later, at the other end: */
x = ntohs(*((unsign ed short *) buffer));
I only need unsigned values, so unsigned {short, long} should be
enough. And one final question: would there be any reason to using
uint16_t and uint32_t instead of unsigned short and unsigned long?
Thanks and sorry for the question avalanche,
Vlad
Comment