Solved

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

Posted on 2008-06-12
6
379 Views
Last Modified: 2013-12-17
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?
0
Comment
Question by:ludy
  • 2
  • 2
6 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 21771947
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;
}
           
 
0
 

Author Comment

by:ludy
ID: 21775124
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?
0
 

Author Comment

by:ludy
ID: 21775282
I mean 5 parameters
0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 100 total points
ID: 21775544
hmmm, I will try to do it by hand:

void spreadIntsToBytes (int[] inInts, int inOff, byte[] outBytes, int outOff, int intLen)
{
      for (int i = 0; i<intLen; i++)
            Array.Copy(BitConverter.GetBytes(inInts[i+inOff]), 0, outBytes, (i+outOff)*4, 4);
}            

void squashBytesToInts (byte[] inBytes, int inOff, int[] outInts, int outOff, int intLen)
{
      for (int i = 0; i<intLen; i++)
            outInts[i+outOff] = BitConverter.ToInt32(inBytes, (i+inOff)*4);
}
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

713 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question