首页 > 编程语言 > 详细

C# Byte数组与Int16数组之间的转换

时间:2017-09-15 17:54:32      阅读:1352      评论:0      收藏:0      [点我收藏+]

有时候,需要类型不同的数组转换,

比如,从采集卡里读出的是Int16类型的数据,需要存储在数据库OLE对象里面,就需要转换成Byte型。

这里提供两个函数,完成相互转换。

        private void Int16ToByte(Int16[] arrInt16, int nInt16Count, ref Byte[] destByteArr)
        {
            //高字节放在前面,低字节放在后面
            for (int i = 0; i < nInt16Count; i++ )
            {
                destByteArr[2 * i + 0] = Convert.ToByte((arrInt16[i] & 0xFF00) >> 8);
                destByteArr[2 * i + 1] = Convert.ToByte((arrInt16[i] & 0x00FF));
            }
        }

        private void ByteToInt16(Byte[] arrByte, int nByteCount, ref Int16[] destInt16Arr)
        {
            //按两个字节一个整数解析,前一字节当做整数高位,后一字节当做整数低位
            for (int i = 0; i < nByteCount / 2; i++)
            {
                destInt16Arr[i] = Convert.ToInt16(arrByte[2 * i + 0] << 8 + arrByte[2 * i + 1]);
            }
        }

C# Byte数组与Int16数组之间的转换

原文:http://www.cnblogs.com/xyzrobot/p/7527034.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!