Endian conversion of signed ints

和自甴很熟 提交于 2019-12-14 01:32:36

问题


I am receiving big endian data over UDP and converting it to little endian. The source says the integers are signed but when I swap the bytes of the signed ints (specifically 16-bit) I get unrealistic values. When I swap them as unsigned ints I get what I expect. I suppose the source documentation could be incorrect and is actually sending unsigned 16-bit ints. But why would that matter? The values are all supposed to be positive and well under 16-bit INT_MAX so overflow should not be an issue. The only thing I can think of is that (1) the documentation is wrong AND (2) I am not handling the sign bit properly when I perform a signed endian swap.

I really have two questions:

1) When overflow is not an issue, does it matter whether I read into signed or unsigned ints.

2) Is endian swapping different between signed and unsigned values (i.e. does the sign bit need to be handled differently)?

I thought endian conversion looked the same for both signed and unsigned values, e.g. for 16-bit value = value&0xff00 >> 8 | value&0x00ff << 8.

Thanks


回答1:


You are running into problems with sign extensions in your swap function. Instead of doing this:

value & 0xff00 >> 8 | value & 0x00ff << 8

do this:

((value >> 8) & 0x00ff) | ((value & 0x00ff) << 8)

The issue is that if value is a 16-bit signed value, then 0xabcd >> 8 is 0xffab. The most significant bit stays 1 if it starts out as 1 in a signed right shift.

Finally, instead of writing this function yourself you should use ntohs().



来源:https://stackoverflow.com/questions/1853369/endian-conversion-of-signed-ints

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!