【问题标题】:Convert byte/int to List<int> reversed and vice versa将 byte/int 转换为 List<int> 反转,反之亦然
【发布时间】:2017-07-29 20:46:30
【问题描述】:

想知道如何以相反的顺序将 int 转换为 List 并用零填充,反之亦然?

有一个字节代表List(8),有时2个字节代表List(16),8个字节代表List(64);所以寻找一个好的解决方案来处理转换为 int 列表,然后再次操作。

例如将 3 输入到 1,1,0,0,0,0,0,0 列表中

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

反之亦然,取 1,1,0,0,0,0,0,0 的列表并返回 3 或 0,1,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);            
    }

【问题讨论】:

  • 修复了这些,但列出了(8)
  • 感谢 Jodrell,需要阅读一下,但已同意 Jean Bob 的回答,因为更适合我的需要。

标签: c# .net


【解决方案1】:

您可以使用按位运算。它们可能很快。

警告:注意 Little/Big Endian (More here)

以下代码有效:

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

【讨论】:

  • 太棒了,我真的很喜欢这个,并且编写了不同的版本来传递 List 和 Offset 来创建更大的列表,并提取列表的子集。非常漂亮和紧凑。谢谢,非常感谢。
【解决方案2】:

这应该可行

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

【讨论】:

    【解决方案3】:

    试试这个

    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;
            }
        }
    }
    ​
    

    【讨论】:

      【解决方案4】:

      OP 的解决方案。

      同意 Jean Bob 的使用 BitWise 的建议。

      为了其他人的利益,这是我的修改版本,以 8 块为单位读取/写入列表。

      private List<int> IntToList(List<int> _List, int _Input)
      {
         int _Padding = 1;
         while (_Padding < 1 << 8)
          {
              _List.Add((_Input & _Padding) == _Padding ? 1 : 0);
              _Padding = _Padding << 1;
          }
          return _List;           
      }
      
      private int IntsToByte(List<int> _List, int l)
      {
          int _Result = 0, _Padding = 0;
          for (int i = l; i < (l + 8); i++)
          {
              _Result = _Result | (_List[i] << _Padding++);
          }
          return _Result;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-03-01
        • 1970-01-01
        • 2020-10-24
        • 2021-06-10
        • 1970-01-01
        • 2011-05-16
        • 2015-02-05
        • 2012-09-18
        相关资源
        最近更新 更多