Click here to Skip to main content
14,743,331 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm looking a way to convert Integer array to byte array in c# in order to work with an existing database design?
Posted
Comments
Mehdi Gholam 13-Sep-15 7:12am
   
Show some code and context of what you mean.

Depends what exactly you are trying to do.
If you have an array of integers and you want to convert it to a stream of bytes so you can regenerate the same array later, then try Buffer.BlockCopy[^]
byte[] bytes = new byte[arrayOfInts.Length * sizeof(int)];
Buffer.BlockCopy(arrayOfInts, 0, bytes, 0, byte.Length);

If you are trying to convert individual values to a byte each then use Linq:
byte[] bytes = arrayOfInts.Select(i => (byte) i).ToArray();
   
Luckily I do exactly this in one of my apps.
Here's the code:

public static byte[] IntArrayToByteArray(int[] ints)
{

    List<byte> bytes = new List<byte>(ints.GetUpperBound(0) * sizeof(byte));

    foreach (int integer in ints)
    {

        bytes.Add(BitConverter.GetBytes(integer)[0]);

    }

    return bytes.ToArray();

}


Some will see this as clunky, however other methods I have tried(don't ask me which ones as since they didn't work I don't remember...) did not always convert the integer to the correct byte representation - there is information on this 'feature' out on google somewhere...
   
v2
Comments
Member 13401733 1-Feb-18 17:27pm
   
You might want to test this code again because it's bugged
GuyThiebaut 2-Feb-18 2:56am
   
I have just tested it an it worked as expected - please provide detail as to how it is 'bugged'.
Member 13401733 4-Feb-18 8:00am
   
class Program
{
static void Main(string[] args)
{
Int16[] ints = new Int16[] {64, 1025, 8193};

byte[] bytes = IntArrayToByteArray(ints);

Debug.Assert(bytes.Length == 6, "3 x Int16 should return 6 bytes");

if (BitConverter.IsLittleEndian)
{
Debug.Assert(bytes[0] == 64 && bytes[1] == 0, "Bytes are NOT in correct order or failed to convert INT16 to bytes");
Debug.Assert(bytes[2] == 4 && bytes[3] == 1, "Bytes are NOT in correct order or failed to convert INT16 to bytes");
Debug.Assert(bytes[4] == 1 && bytes[3] == 32, "Bytes are NOT in correct order or failed to convert INT16 to bytes");
}

}

public static byte[] IntArrayToByteArray(Int16[] ints)
{

List<byte> bytes = new List<byte>(ints.GetUpperBound(0) * sizeof(byte));

foreach (Int16 integer in ints)
{

bytes.Add(BitConverter.GetBytes(integer)[0]);

}

return bytes.ToArray();

}
}
//Covert To byte Array

public byte[] ConvertIntArrayToByteArray(int[] inputElements)
      {
          byte[] myFinalBytes = new byte[inputElements.Length * 4];
          for(int cnt = 0 ; cnt<inputElements.Length; cnt ++)
          {
              byte[] myBytes = BitConverter.GetBytes(inputElements[cnt]);
              Array.Copy(myBytes, 0, myFinalBytes, cnt*4, 4);
          }
          return myFinalBytes;
      }

//Convert To Integer Array
public int[] ConvertToInt32Array(byte[] inputElements)
       {
           int[] myFinalIntegerArray = new int[inputElements.Length / 4];
           for (int cnt = 0; cnt < inputElements.Length; cnt += 4)
           {
               myFinalIntegerArray[cnt / 4] = BitConverter.ToInt32(inputElements, cnt);
           }
           return myFinalIntegerArray;
       }


//Call from your application:
byte[] myFinalBytes= test.ConvertIntArrayToByteArray(inputElements);
           int [] myIntegerArray = test.ConvertToInt32Array(myFinalBytes);


Help Link For BitConverter Class <a href="https://msdn.microsoft.com/en-us/library/de8fssa4(v=vs.110).aspx">https://msdn.microsoft.com/en-us/library/de8fssa4(v=vs.110).aspx</a>[<a href="https://msdn.microsoft.com/en-us/library/de8fssa4(v=vs.110).aspx" target="_blank" title="New Window">^</a>]
   

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