Forum guidelines
AuthorQuestion
Converting an array of int to an array of bytes
Posted on: 12 Aug 2007

Is there a way of converting a int array into a byte array. In C you could build a union an just fill the int array. I also need to go from a byte array to a int array after transmission.


AuthorReply
Alan
  • 0
  • 0
Re: Converting an array of int to an array of bytes
Posted on: 12 Aug 2007  

In C#, I think most people would just do something like this:

        int[] ia = new int[someSize];   
        // code to initialize ia
        byte[] ba = new byte[4 * someSize];

        for(int i = 0; i < ia.Length ; i++)
        {
            int j = 4 * i;
            ba[j] = (byte)(ia[i] & 0xFF);
            ba[j+1] = (byte)(ia[i] >> 8 & 0xFF);
            ba[j+2] = (byte)(ia[i] >>16 & 0xFF);
            ba[j+3] = (byte)(ia[i] >> 24 & 0xFF);
       }

You can also use the BitConverter.GetBytes(someInt) method to get the byte representation of a single integer though using this class isn't as fast as bit arithmetic. 

Although it's possible to simulate unions, you can't overlap reference types within them which, of course, is what arrays are in .NET. You might be able to do something using pointers in 'unsafe' code but I doubt whether it would be much quicker than the above.


Re: Converting an array of int to an array of bytes
Posted on: 12 Aug 2007  
Thanks I was trying that but was missing the type cast. what about the other direction byte array to int anything there????
Alan
  • 0
  • 0
Re: Converting an array of int to an array of bytes
Posted on: 12 Aug 2007  

Sorry, I forgot about that one :)

        int[] ia  = new int[ba.Length / 4];
        for(int i = 0;  i < ia.Length;  i++)
        {
           int j = 4 * i;
           ia[i] = ((int)ba[j+3] << 24) + ((int)ba[j+2] << 16) + ((int)ba[j+1] << 8) + ((int)ba[j]);
        }


Re: Converting an array of int to an array of bytes
Posted on: 12 Aug 2007  
The better question in this case is is there a method that converts 4 bytes into an int in one step. ie convert array elements 2,3,4,5 to a int?????
Alan
  • 0
  • 0
Re: Converting an array of int to an array of bytes
Posted on: 12 Aug 2007  

You can use:

int i  = BitConverter.ToInt32(ba, 2);

which takes the 4 bytes of the byte array starting at index 2 (zero-based) and converts them to an int.


Re: Converting an array of int to an array of bytes
Posted on: 12 Aug 2007  
Thanks again I was trying Convert.toInt but that wasn't working
Alan
  • 0
  • 0
Re: Converting an array of int to an array of bytes
Posted on: 12 Aug 2007  

Convert.ToInt32(byteValue)  is equivalent to casting the byte value to an int.

So, if you have a byte with a value of 32, it just 'widens' it to a 4 byte Int32 containing the same value.  


Re: Converting an array of int to an array of bytes
Posted on: 12 Aug 2007  

your right, but the values in the array span byte boundries i.e. the fist value is contained in the first 9 bits. So it eisier to take in multiple bytes and shift left and right to get the value. Thanks again.


Employers - Post Free Jobs

SPONSORED BY

Custom Software Development
MCN is your source for developing solutions involving websites, mobile apps, cloud-computing, databases, BI, back-end services and processes and client-server applications.