C# 将byte/int转换为List<;int>;反之亦然

C# 将byte/int转换为List<;int>;反之亦然,c#,.net,C#,.net,我想知道如何将一个int转换成一个以零填充的逆序列表,反之亦然 有一个字节表示列表(8),有时2个字节表示列表(16),8个字节表示列表(64);因此,寻找一个好的解决方案来处理转换为int列表,然后再进行操作 e、 g.在1,1,0,0,0,0,0的列表中输入3 或将42输入到0,1,0,1,0,1,0,0的列表中 反之亦然,取1,1,0,0,0,0,0,0的列表并返回3,或取0,1,0,1,0,0的列表并返回42 目前我所做的是构建两个函数来处理这两种情况,所有这些都很好,只是想知道是否有一

我想知道如何将一个int转换成一个以零填充的逆序列表,反之亦然

有一个字节表示列表(8),有时2个字节表示列表(16),8个字节表示列表(64);因此,寻找一个好的解决方案来处理转换为int列表,然后再进行操作

e、 g.在1,1,0,0,0,0,0的列表中输入3

或将42输入到0,1,0,1,0,1,0,0的列表中

反之亦然,取1,1,0,0,0,0,0,0的列表并返回3,或取0,1,0,1,0,0的列表并返回42

目前我所做的是构建两个函数来处理这两种情况,所有这些都很好,只是想知道是否有一个更好/更优雅的解决方案我完全没有忽略

    private List<int> IntToList(int _Input)
    {
        string _Binary = ReverseString(Convert.ToString(_Input, 2).PadLeft(8, '0'));
        List<int> _List = new List<int>(8);
        for (int i = 0; i < _Binary.Length; i++)
        {
            _List.Add(Convert.ToInt32(_Binary.Substring(i, 1)));
        }
        return _List;            
    }

    private int IntsToByte(List<int> _List)
    {
        string _Binary = "";
        for (int i = 7; i > -1; i--)
        {
            _Binary += _List[i];
        }
        return Convert.ToInt32(_Binary, 2);            
    }
private List IntToList(int\u输入)
{
字符串_Binary=ReverseString(Convert.ToString(_Input,2).PadLeft(8,'0'));
列表_List=新列表(8);
对于(int i=0;i<\u Binary.Length;i++)
{
_添加(Convert.ToInt32(_Binary.Substring(i,1));
}
返回列表;
}
私有int IntsToByte(列表_列表)
{
字符串_Binary=“”;
对于(int i=7;i>-1;i--)
{
_二进制+=_列表[i];
}
返回Convert.ToInt32(_Binary,2);
}
这应该行得通

int number = 42
char[] reverse = Convert.ToString(number, 2).PadLeft(8, '0').ToCharArray();
Array.Reverse(reverse);
这应该行得通

int number = 42
char[] reverse = Convert.ToString(number, 2).PadLeft(8, '0').ToCharArray();
Array.Reverse(reverse);
这应该行得通

int number = 42
char[] reverse = Convert.ToString(number, 2).PadLeft(8, '0').ToCharArray();
Array.Reverse(reverse);
这应该行得通

int number = 42
char[] reverse = Convert.ToString(number, 2).PadLeft(8, '0').ToCharArray();
Array.Reverse(reverse);

您可以使用按位操作。他们可能很快

警告:注意小/大端()

以下代码起作用:

  private List<int> IntToList(int _Input, int _MaxSize = 8)
  {
    int padding = 1;
    List<int> resultList = new List<int>(_MaxSize);
    while (padding < 1 << _MaxSize)
      {
        resultList.Add((_Input & padding) == padding ? 1 : 0);
        padding = padding << 1;
      }
    return resultList;            
  }

  private int IntsToByte(List<int> _List)
  {
    int result = 0, padding = 0;
    foreach (int i in _List)
    {
        result = result | (i << padding++);
    }
    return result;            
  }
private List IntToList(int-Input,int-MaxSize=8)
{
int padding=1;
列表结果列表=新列表(\u MaxSize);

while(padding<1您可以使用位操作。它们可能很快

警告:注意小/大端()

以下代码起作用:

  private List<int> IntToList(int _Input, int _MaxSize = 8)
  {
    int padding = 1;
    List<int> resultList = new List<int>(_MaxSize);
    while (padding < 1 << _MaxSize)
      {
        resultList.Add((_Input & padding) == padding ? 1 : 0);
        padding = padding << 1;
      }
    return resultList;            
  }

  private int IntsToByte(List<int> _List)
  {
    int result = 0, padding = 0;
    foreach (int i in _List)
    {
        result = result | (i << padding++);
    }
    return result;            
  }
private List IntToList(int-Input,int-MaxSize=8)
{
int padding=1;
列表结果列表=新列表(\u MaxSize);

while(padding<1您可以使用位操作。它们可能很快

警告:注意小/大端()

以下代码起作用:

  private List<int> IntToList(int _Input, int _MaxSize = 8)
  {
    int padding = 1;
    List<int> resultList = new List<int>(_MaxSize);
    while (padding < 1 << _MaxSize)
      {
        resultList.Add((_Input & padding) == padding ? 1 : 0);
        padding = padding << 1;
      }
    return resultList;            
  }

  private int IntsToByte(List<int> _List)
  {
    int result = 0, padding = 0;
    foreach (int i in _List)
    {
        result = result | (i << padding++);
    }
    return result;            
  }
private List IntToList(int-Input,int-MaxSize=8)
{
int padding=1;
列表结果列表=新列表(\u MaxSize);

while(padding<1您可以使用位操作。它们可能很快

警告:注意小/大端()

以下代码起作用:

  private List<int> IntToList(int _Input, int _MaxSize = 8)
  {
    int padding = 1;
    List<int> resultList = new List<int>(_MaxSize);
    while (padding < 1 << _MaxSize)
      {
        resultList.Add((_Input & padding) == padding ? 1 : 0);
        padding = padding << 1;
      }
    return resultList;            
  }

  private int IntsToByte(List<int> _List)
  {
    int result = 0, padding = 0;
    foreach (int i in _List)
    {
        result = result | (i << padding++);
    }
    return result;            
  }
private List IntToList(int-Input,int-MaxSize=8)
{
int padding=1;
列表结果列表=新列表(\u MaxSize);
而(填充<1试试这个

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            List<ulong> results = null;
            List<byte> output = null;
            List<byte> input1 = new List<byte>() { 1, 1, 0, 0, 0, 0, 0, 0 };
            results = ReadList(input1, 1);
            output = WriteList(results,1);
            List<byte> input2 = new List<byte>() { 0, 1, 0, 1, 0, 1, 0, 0 };
            results = ReadList(input2, 1);
            output = WriteList(results,1);

        }
        static List<ulong> ReadList(List<byte> input, int size)
        {
            List<ulong> results = new List<ulong>();
            input.Reverse();
            MemoryStream stream = new MemoryStream(input.ToArray());
            BinaryReader reader = new BinaryReader(stream);
            int count = 0;
            ulong newValue = 0;
            while (reader.PeekChar() != -1)
            {

                switch (size)
                {
                    case 1:
                        newValue = ((ulong)Math.Pow(2, size) * newValue) + (ulong)reader.ReadByte();
                        break;
                    case 2:
                        newValue = ((ulong)Math.Pow(2, size) * newValue) + (ulong)reader.ReadInt16();
                        break;
                }
                if (++count == size)
                {
                    results.Add(newValue);
                    newValue = 0;
                    count = 0;
                }
            }
            return results;
        }
        static List<byte> WriteList(List<ulong> input, int size)
        {
            List<byte> results = new List<byte>();
            foreach (ulong num in input)
            {
                ulong result = num;
                for (int count = 0; count < size; count++)
                {
                    if (result > 0)
                    {
                        byte bit = (byte)(result % Math.Pow(2, size));
                        results.Add(bit);
                        result = (ulong)(result / Math.Pow(2, size));
                    }
                    else
                    {
                        results.Add(0);
                    }
                }
            }
            results.Reverse();
            return results;
        }
    }
}
​
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.IO;
命名空间控制台应用程序1
{
班级计划
{
静态void Main(字符串[]参数)
{
列表结果=空;
列表输出=null;
List input1=新列表(){1,1,0,0,0,0,0};
结果=读取列表(输入1,1);
输出=写列表(结果,1);
List input2=新列表(){0,1,0,1,0,1,0};
结果=读取列表(输入2,1);
输出=写列表(结果,1);
}
静态列表读取列表(列表输入,整数大小)
{
列表结果=新列表();
input.Reverse();
MemoryStream stream=新的MemoryStream(input.ToArray());
BinaryReader=新的BinaryReader(流);
整数计数=0;
ulong newValue=0;
while(reader.PeekChar()!=-1)
{
开关(尺寸)
{
案例1:
newValue=((ulong)Math.Pow(2,大小)*newValue)+(ulong)reader.ReadByte();
打破
案例2:
newValue=((ulong)Math.Pow(2,size)*newValue)+(ulong)reader.ReadInt16();
打破
}
如果(++计数==大小)
{
结果:增加(新值);
newValue=0;
计数=0;
}
}
返回结果;
}
静态列表写入列表(列表输入,整数大小)
{
列表结果=新列表();
foreach(输入中的ulong num)
{
ulong result=num;
对于(int count=0;count0)
{
字节位=(字节)(结果%Math.Pow(2,大小));
结果。添加(位);
结果=(ulong)(结果/数学功率(2,大小));
}
其他的
{
结果:添加(0);
}
}
}
结果:反向();
返回结果;
}
}
}
​
试试这个

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            List<ulong> results = null;
            List<byte> output = null;
            List<byte> input1 = new List<byte>() { 1, 1, 0, 0, 0, 0, 0, 0 };
            results = ReadList(input1, 1);
            output = WriteList(results,1);
            List<byte> input2 = new List<byte>() { 0, 1, 0, 1, 0, 1, 0, 0 };
            results = ReadList(input2, 1);
            output = WriteList(results,1);

        }
        static List<ulong> ReadList(List<byte> input, int size)
        {
            List<ulong> results = new List<ulong>();
            input.Reverse();
            MemoryStream stream = new MemoryStream(input.ToArray());
            BinaryReader reader = new BinaryReader(stream);
            int count = 0;
            ulong newValue = 0;
            while (reader.PeekChar() != -1)
            {

                switch (size)
                {
                    case 1:
                        newValue = ((ulong)Math.Pow(2, size) * newValue) + (ulong)reader.ReadByte();
                        break;
                    case 2:
                        newValue = ((ulong)Math.Pow(2, size) * newValue) + (ulong)reader.ReadInt16();
                        break;
                }
                if (++count == size)
                {
                    results.Add(newValue);
                    newValue = 0;
                    count = 0;
                }
            }
            return results;
        }
        static List<byte> WriteList(List<ulong> input, int size)
        {
            List<byte> results = new List<byte>();
            foreach (ulong num in input)
            {
                ulong result = num;
                for (int count = 0; count < size; count++)
                {
                    if (result > 0)
                    {
                        byte bit = (byte)(result % Math.Pow(2, size));
                        results.Add(bit);
                        result = (ulong)(result / Math.Pow(2, size));
                    }
                    else
                    {
                        results.Add(0);
                    }
                }
            }
            results.Reverse();
            return results;
        }
    }
}
​
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.IO;
命名空间控制台应用程序1
{
班级计划
{
静态void Main(字符串[]参数)
{
列表结果=空;
列表输出=null;
List input1=新列表(){1,1,0,0,0,0,0};
结果=读取列表(输入1,1);
输出=写列表(结果,1);
List input2=新列表(){0,1,0,1,0,1,0};
结果=读取列表(输入2,1);
输出=写列表(结果,1);
}
静态列表读取列表(列表输入,整数大小)
{
列表结果=新列表();
input.Reverse();
MemoryStream stream=新的MemoryStream(input.ToArray());
BinaryReader=新的BinaryReader(流);
整数计数=0;
乌龙新价值