endianness - Endian conversion of signed ints -
I am getting big Andian data on UDP and am converting it to a little bit. The source says that integers are signed, but when I swap signed bytes (especially 16-bit) bytes, I get unrealistic value. When I swap them as unsigned deficiencies, I Hopefully what I hope. I think the source documentation might be wrong and actually sending unsigned 16-bit ints, but why would this happen? Values are considered well under all positive and 16-bit INT_MAX, so overflow should not be problem. The only thing I can think about is (1) the documentation is wrong and (2) when I swap the signed intraconship, I do not keep the sign bit correctly.
I have really two questions:
1) When overflow is not a problem, does it make any difference whether I read it in signed or unsigned entities?
2) Is it necessary to handle different bit differently between unsigned and unsigned values?)
I thought that the endian conversion looks similar to both signed and unsigned values, e.g. 16-bit value = value & amp; 0xff00 & gt; & Gt; 8 | Value & amp; 0x00ff & lt; & Lt; 8
.
Thank you
You are running problems with hints in your swap function Instead of doing this extension:
Pricing & amp; 0xff00 & gt; & Gt; 8 | Price & amp; 0x00ff & lt; & Lt; 8
Do this:
((value> 8) & amp; 0x00ff). (Value & amp; 0x00ff) & lt; & Lt; 8) The issue is that if value
is a 16-bit signed value, then 0xabcd> & Gt; 8
is 0xffab
. The most important bit remains 1 if it starts in signed right shift as 1. Finally, instead of writing this function yourself, you should use the ntohs ()
.
Comments
Post a Comment