将16位有符号整数,以2个字节? [英] Convert 16-bit signed int to 2-bytes?
本文介绍了将16位有符号整数,以2个字节?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我得到了已被窃听我一个简单的问题,我可以找到一个解决的办法。我得到了其中包含符号int数据的数组,我需要每个值在数组中转换为2个字节。我使用C#和我尝试使用BitConverter.GetBytes(INT),但它返回一个4字节数组。
Hi i got a simple problem that has been bugging me and i can find a solution to it. I got an array which contains signed int data, i need to convert each value in the array to 2 bytes. I am using C# and i tried using BitConverter.GetBytes(int) but it returns a 4 byte array.
任何帮助吗?
感谢特里斯坦
推荐答案
一个符号的16位值作为一个短
,而不是 INT
- 所以使用 BitConverter.GetBytes(短)
< 。/ A>
A signed 16-bit value is best represented as a short
rather than int
- so use BitConverter.GetBytes(short)
.
然而,作为一种替代方案:
However, as an alternative:
byte lowByte = (byte) (value & 0xff);
byte highByte = (byte) ((value >> 8) & 0xff);
这篇关于将16位有符号整数,以2个字节?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文