Link to home
Start Free TrialLog in
Avatar of ludy
ludy

asked on

How to convert Java's squashBytesToInts() and spreadIntsToBytes() functions in C Sharp?

I'm converting Java code to C-sharp and came across squashBytesToInts and spreadIntsToBytes which is not built-in in C-Sharp. How is it done in C Sharp?
Avatar of Jaime Olivares
Jaime Olivares
Flag of Peru image

there are many ways to do it, you can use BitConverter:

static byte[] spreadIntsToBytes(int[] ints)
{
      byte[] result = new byte[ints.Length*4];
      for (int i = 0; i<ints.Length; i++)
            Array.Copy( BitConverter.GetBytes(ints[i]), 0, result, i*4, 4);
      return result;
}            
static int[] squashBytesToInts(byte[] bytes)
{
      int size = bytes.Length >> 2;  // divide by 4
      int[] result = new int[size];
      for (int i = 0; i<size; i++)
            int[i] = BitConverter.ToInt32(bytes, i*4);
      return result;
}
           
 
Avatar of ludy
ludy

ASKER

Thanks but, the functions used are presented with four parameters:

squashBytesToInts (byte[] inBytes, int inOff, int[] outInts, int outOff, int intLen)
spreadIntsToBytes (int[] inInts, int inOff, byte[] outBytes, int outOff, int intLen)

How would it translate given these four parameters?
Avatar of ludy

ASKER

I mean 5 parameters
ASKER CERTIFIED SOLUTION
Avatar of Jaime Olivares
Jaime Olivares
Flag of Peru image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial