Solved

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

Posted on 2008-06-12
6
369 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
Comment Utility
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
Comment Utility
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
Comment Utility
I mean 5 parameters
0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 100 total points
Comment Utility
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

762 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now