0

Was wondering how can I convert an int to a List in reverse order padded with zeroes and vice versa?

Have a byte that represents List(8), sometimes 2 bytes for List(16), 8 bytes for List(64); so looking for a good solution to handle converting to an int list, manipulate then back again.

e.g. Input of 3 to a List of 1,1,0,0,0,0,0,0

Or input of 42 to a List of 0,1,0,1,0,1,0,0

And vice-versa, take a List of 1,1,0,0,0,0,0,0 and return 3 or List of 0,1,0,1,0,1,0,0 and return 42

What I have done at present is build a couple of functions to handle both scenarios, all works fine, just wondering if there is a better / more elegant solution that I've completelt overlooked?

    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);            
    }
Cœur
  • 37,241
  • 25
  • 195
  • 267
user3295596
  • 109
  • 2
  • 8

4 Answers4

3

You can work with bitwise operations. They might be fast.

Warning : Be aware of Little/Big Endian (More here)

The following code works :

  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;            
  }
Community
  • 1
  • 1
Jean Bob
  • 565
  • 10
  • 24
  • Awesome, I really like this one, and have written different versions to pass in List and an Offset to create larger lists, and extract subsets of the list. Very nice and compact. Thank you, much appreciated. – user3295596 Aug 12 '15 at 06:32
0

This should work

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

Try this

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;
        }
    }
}
​
jdweng
  • 33,250
  • 2
  • 15
  • 20
0

Solution from OP.

Have gone with Jean Bob's suggestion of using BitWise.

For anyone elses benefit, here is my modified version to read / write in blocks of 8 to/from the list.

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;
}
Cœur
  • 37,241
  • 25
  • 195
  • 267