#region 将整型写入字节数组中(最大四位)
/// <summary>
/// 将整型写入字节数组中(最大四位)
/// 超过四位时需要调整nStartIndex
/// </summary>
/// <param name="cbData">要写入的数据源</param>
/// <param name="nStartIndex">开始写入的位置</param>
/// <param name="nSourceValue">要写入的数值</param>
/// <param name="nWriteSize">写入的大小</param>
#endregion
private static void WriteIntToByteArray(Byte[] cbData, Int32 nStartIndex, UInt64 nSourceValue, Int32 nWriteSize)
{
//大小校验
if (nWriteSize <= 0 || nWriteSize > 8) return;
//位运算掩码
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 };
//循环写入
for (Int32 i = nWriteSize - 1; i >= 0; i--)
{
cbData[nStartIndex + i] = (Byte)((nSourceValue & nCodeValue[i]) >> nCodeRight[i]);
/* 默认高低位互换,若不互换,使用下面语句
* cbData[nStartIndex + nWriteSize - 1 - i] = (Byte)((nSourceValue & nCodeValue[i]) >> nCodeRight[i]);
* */
}
}
/// <summary>
/// 将整型写入字节数组中(最大四位)
/// 超过四位时需要调整nStartIndex
/// </summary>
/// <param name="cbData">要写入的数据源</param>
/// <param name="nStartIndex">开始写入的位置</param>
/// <param name="nSourceValue">要写入的数值</param>
/// <param name="nWriteSize">写入的大小</param>
#endregion
private static void WriteIntToByteArray(Byte[] cbData, Int32 nStartIndex, UInt64 nSourceValue, Int32 nWriteSize)
{
//大小校验
if (nWriteSize <= 0 || nWriteSize > 8) return;
//位运算掩码
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 };
//循环写入
for (Int32 i = nWriteSize - 1; i >= 0; i--)
{
cbData[nStartIndex + i] = (Byte)((nSourceValue & nCodeValue[i]) >> nCodeRight[i]);
/* 默认高低位互换,若不互换,使用下面语句
* cbData[nStartIndex + nWriteSize - 1 - i] = (Byte)((nSourceValue & nCodeValue[i]) >> nCodeRight[i]);
* */
}
}