一下是代码片段
public class CBytes
{
protected byte [] bytes; public CBytes(int numberOfBytes)
{
bytes = new byte[numberOfBytes];
} public CBytes(byte[] bytes)
{
this.bytes = bytes;
} public virtual byte [] Bytes
{
get
{
return this.bytes;
}
}
protected void GetBits(int mostSignificantByteIndex, int mostSignificantStartBit, int numberOfBits, int numberOfBitsToShift, out ulong bitValues)
{
bitValues = 0; int numberOfBitsToHandle; while(numberOfBits > 0)
{
numberOfBitsToHandle = ((mostSignificantStartBit+1)>=numberOfBits)?numberOfBits:(mostSignificantStartBit+1); byte b = bytes[mostSignificantByteIndex]; for(int i=0; i<numberOfBitsToHandle; i++)
{
bitValues <<= 1; if( (b & 1 <<(mostSignificantStartBit - i)) != 0)
{
bitValues |= 1;
}
} mostSignificantStartBit = 7;
numberOfBits -= numberOfBitsToHandle;
mostSignificantByteIndex++;
} bitValues <<= numberOfBitsToShift;
}
} mostSignificantStartBit = 7;
numberOfBits -= numberOfBitsToHandle;
mostSignificantByteIndex++;
} bitValues <<= numberOfBitsToShift;
}
如果我知道其中GetBits()这个方法是做什么用的,我看好像是用来由10进制转为16进制,不知正确否?
如果是,这个方法中的每一步的作用是什么?可否给详细讲解一下,非常感谢。
public class CBytes
{
protected byte [] bytes; public CBytes(int numberOfBytes)
{
bytes = new byte[numberOfBytes];
} public CBytes(byte[] bytes)
{
this.bytes = bytes;
} public virtual byte [] Bytes
{
get
{
return this.bytes;
}
}
protected void GetBits(int mostSignificantByteIndex, int mostSignificantStartBit, int numberOfBits, int numberOfBitsToShift, out ulong bitValues)
{
bitValues = 0; int numberOfBitsToHandle; while(numberOfBits > 0)
{
numberOfBitsToHandle = ((mostSignificantStartBit+1)>=numberOfBits)?numberOfBits:(mostSignificantStartBit+1); byte b = bytes[mostSignificantByteIndex]; for(int i=0; i<numberOfBitsToHandle; i++)
{
bitValues <<= 1; if( (b & 1 <<(mostSignificantStartBit - i)) != 0)
{
bitValues |= 1;
}
} mostSignificantStartBit = 7;
numberOfBits -= numberOfBitsToHandle;
mostSignificantByteIndex++;
} bitValues <<= numberOfBitsToShift;
}
} mostSignificantStartBit = 7;
numberOfBits -= numberOfBitsToHandle;
mostSignificantByteIndex++;
} bitValues <<= numberOfBitsToShift;
}
如果我知道其中GetBits()这个方法是做什么用的,我看好像是用来由10进制转为16进制,不知正确否?
如果是,这个方法中的每一步的作用是什么?可否给详细讲解一下,非常感谢。
我先睡一会,有高人看到就解释一下吧。
using System;namespace CByteTest
{
class Program
{
static void Main(string[] args)
{
CBytes cb = new CBytes(new byte[] { 63, 127 });
// 计算cb.bytes第一位置数字的bit位数
int mostSignificantStartBit = Convert.ToString(cb.Bytes[0], 2).Length; foreach (byte b in cb.Bytes)
{
Console.Write(b.ToString() + ",");
}
Console.WriteLine();
ulong result = 0;
cb.GetBits(0, mostSignificantStartBit, mostSignificantStartBit + (cb.Bytes.Length - 1) * 7, 0, out result);
Console.WriteLine(result); Console.ReadKey();
}
}
public class CBytes
{
protected byte[] bytes; public CBytes(int numberOfBytes)
{
bytes = new byte[numberOfBytes];
} public CBytes(byte[] bytes)
{
this.bytes = bytes;
} public virtual byte[] Bytes
{
get
{
return this.bytes;
}
} /// <summary>
/// 用bitwise operator将128进制转换为10进制,并进行bitwise shift
/// </summary>
/// <param name="mostSignificantByteIndex">最高位的起始位置,即bytes数列的第一个非0数字,例如{63,127},63的位置是0</param>
/// <param name="mostSignificantStartBit">最高位的bit位数,例如63,二进制为111111,共6位,起始位数为6</param>
/// <param name="numberOfBits">二进制总共位数,例如{63,127} 为 6+7=13位</param>
/// <param name="numberOfBitsToShift">向左bitwise shift的位数</param>
/// <param name="bitValues">转换结果</param>
public void GetBits(int mostSignificantByteIndex, int mostSignificantStartBit, int numberOfBits, int numberOfBitsToShift, out ulong bitValues)
{
bitValues = 0; int numberOfBitsToHandle = 0; while (numberOfBits > 0)
{
numberOfBitsToHandle = (mostSignificantStartBit >= numberOfBits) ? numberOfBits : mostSignificantStartBit; byte b = bytes[mostSignificantByteIndex]; // 从cb.bytes[0]开始,每次循环处理一位128进制数字
for (int i = 0; i < numberOfBitsToHandle; i++)
{
// 因为我们是从大到小循环,每次要想左shift一位
bitValues <<= 1; // ...
if ((b & 1 << (mostSignificantStartBit - i - 1)) != 0)
{
bitValues |= 1;
}
} mostSignificantStartBit = 7;
numberOfBits -= numberOfBitsToHandle;
mostSignificantByteIndex++;
} bitValues <<= numberOfBitsToShift;
}
}
}