从字节数组中读取整型(最大四位)

#region 从字节数组中读取整型(最大四位)
        /// <summary>
        /// 从字节数组中读取整型(最大四位)
        /// 超过四位时需要调整nStartIndex
        /// </summary>
        /// <param name="cbData">要读取的数据源</param>
        /// <param name="nStartIndex">开始读取的位置</param>
        /// <param name="nReadSize">读取的大小</param>
        /// <returns>以Int32型返回读取到的数值</returns>
        #endregion
        private static Int64 ReadIntFromByteArray(Byte[] cbData, Int32 nStartIndex, Int32 nReadSize)
        {
            //大小校验
            if (nReadSize <= 0 || nReadSize > 8) return 0;


            //位运算掩码
            Int32[] nCodeRight = new Int32[8] { 0, 8, 16, 24, 32, 40, 48, 56 };
            UInt64[] nCodeValue = new UInt64[8] { 0xFF, 0xFF00, 0xFF0000, 0xFF000000, 0xFF00000000, 0xFF0000000000, 0xFF000000000000, 0xFF00000000000000 };


            //返回结果定义
            UInt64 nResult = 0;


            //循环读取
            for (Int32 i = 0; i < nReadSize; i++)
            {
                /* 默认高低位互换,若不互换,使用下面语句
                 * Int32 nData = cbData[nStartIndex + i];
                 * */
                UInt64 nData = cbData[nStartIndex + nReadSize - 1 - i];
                nResult |= (UInt64)((nData << nCodeRight[nReadSize - 1 - i]) & nCodeValue[nReadSize - 1 - i]);
            }


            return (Int64)nResult;
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值