Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a List/array with some integers.
array = {1,2,3,4,5,7,11,14,15...}
for each integer that exists in the array/list from 1 to 8 i append a bit until 1 byte and then convert the binary to decimal. In this case 11111010(6 and 8 does not exist) = 250. Likewise the next iteration.

What I have tried:

I tried it with appending each bit to a string when the number occurs and then converting it to decimal. Once all 8 iterations done, convert it into an 8 byte array.
Is there a better way to do it? perhaps using bitwise operators?

C#
private byte[] GetBitMapFromFields(List<int> fieldNo)
        {
            byte[] bytes = new byte[8];
            List<int> tempList = new List<int>();
            string binaryfield = string.Empty;
            var count = 1;
            for (var i = 0; i < fieldNo.Count; i++)
            {
                var k = 0;
                for(var j =1; j<=8;j++)
                {
                    if (count == fieldNo[i])
                    {
                        binaryfield = binaryfield + 1;
                        i++;
                        k++;
                        count++;
                    }
                    else
                    {
                        binaryfield = binaryfield + 0;
                        k++;
                        count++;
                    }
                    if(k==8)
                    {
                        i--;
                        break;
                    }
                }
                int output = Convert.ToInt32(binaryfield, 2);
                binaryfield = string.Empty;
                tempList.Add(output);

            }
            bytes = ConvertListToByte(tempList); //Converts the list to byte array of length 8
            return bytes;
        }
Posted
Updated 16-May-20 11:40am

Something like the following will capture eight bits at a time:
C++
int pattern = 0;

for ( // expression to iterate over your list
{
    if ( // if the value exists
    {
        pattern |= 1;
    }
    pattern <<= 1;
}

// pattern now contains the mask of the items.

You also need to break the loop after 8 test items. Alternatively you could capture 32 or 64 bits at a time.
 
Share this answer
 

I did something similar when encrypting data using the BitArray class. Hope this is of some help


C#
static void Main(string[] args)
       {
           List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 7 };//, 11, 14, 15
           int max = numbers.Max();
           int mod8 = max % 8;
           int length = mod8 == 0 ? max : max + 8 - mod8;
           BitArray bits = new BitArray(length);
           foreach (var n in numbers) bits[length - n] = true;
           int[] tempIntArray = new int[1];
           bits.CopyTo(tempIntArray, 0);
           int result = tempIntArray[0];

           Console.ReadLine();
       }
 
Share this answer
 
Comments
Jacxx 17-May-20 8:33am    
Thank you George, that solution did work. However, instead of int[] i converted the bits directly to byte[] -
byte[] byteArray = new byte[(int)Math.Ceiling((double)bits.Length / 8)];
bits.CopyTo(byteArray, 0);
Also had to reverse it.
Your code to set bits look rather complicated.
First of all you need to know that an integer is stored in base 2, the integer is a field of bits.
example of 10!
C#
10= 1*2^3+0*2^2+1*2^1+0*2^0

bits are numbered from right to left, starting with bit 0 and number match the powers of 2 from previous formula.
C#
bit numbers: 7 6 5 4 3 2 1 0
Values:                    ^ = 1 = 2^0
                         ^ = 2 = 2^1
                       ^ = 4 = 2^2
                      ^ = 8 = 2^3
...
              ^ = 128 = 2^7
10 is         0 0 0 0 1 0 1 0

To set high bits in the 8 bits, you just have to use the knowledge of their values and know bitwise operations.
a single loop does the trick:
C#
int set8bits (int value) { // set high bits
    int bit= 128;
    for ( int i=7, i>=0, i--) {
        if (value & bit) { // is true when bit is already set
            break;
        }
        value = value | bit; // set bit
        bit= bit >> 1 // move next bit
    }
    return value;
}
 
Share this answer
 
v2
Comments
Jacxx 17-May-20 8:39am    
Thank you so much for the explanation Patrice. This was the solution i was after. I'm now checking if this one or George's answer performs better.
Patrice T 17-May-20 9:11am    
If question is solved, accept useful solutions, it will close the question.
Patrice T 17-May-20 9:13am    
My solution should be more efficient because it avoid transformations.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900