C# 移位字节
现在我有一个7位字节的数组,或者一个字符串,我想从第二个字节中取最后一位,然后将它添加到第一个字节的最右边,依此类推,这将产生一个新的8位字节,除了使用循环和数组,还有什么直接的方法可以做到这一点吗? 例子 我怎样才能通过c#做到这一点?C# 移位字节,c#,.net,C#,.net,现在我有一个7位字节的数组,或者一个字符串,我想从第二个字节中取最后一位,然后将它添加到第一个字节的最右边,依此类推,这将产生一个新的8位字节,除了使用循环和数组,还有什么直接的方法可以做到这一点吗? 例子 我怎样才能通过c#做到这一点? 谢谢。这里有一个简单的程序,可以满足该页面的要求: public class Septets { readonly List<byte> _bytes = new List<byte>(); private int _c
谢谢。这里有一个简单的程序,可以满足该页面的要求:
public class Septets
{
readonly List<byte> _bytes = new List<byte>();
private int _currentBit, _currentByte;
void EnsureSize(int index)
{
while (_bytes.Count < index + 1)
_bytes.Add(0);
}
public void Add(bool bitVal)
{
EnsureSize(_currentByte);
if (bitVal)
_bytes[_currentByte] |= (byte)(1 << _currentBit);
_currentBit++;
if (_currentBit == 8)
{
_currentBit = 0;
_currentByte++;
}
}
public void AddSeptet(byte septet)
{
for (int n = 0; n < 7; n++)
Add(((septet & (1 << n)) != 0 ? true : false));
}
public void AddSeptets(byte[] septets)
{
for (int n = 0; n < septets.Length; n++)
AddSeptet(septets[n]);
}
public byte[] ToByteArray()
{
return _bytes.ToArray();
}
public static byte[] Pack(byte[] septets)
{
var packer = new Septets();
packer.AddSeptets(septets);
return packer.ToByteArray();
}
}
static void Main(string[] args)
{
byte[] text = Encoding.ASCII.GetBytes("hellohello");
byte[] output = Septets.Pack(text);
for (int n = 0; n < output.Length; n++)
Console.WriteLine(output[n].ToString("X"));
}
E8 32 9B FD 46 97 D9 EC 37