#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;
}
/// <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;
}