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

Posted on 2008-06-12
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?
Question by:ludy
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
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;

Author Comment

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?

Author Comment

ID: 21775282
I mean 5 parameters
LVL 55

Accepted Solution

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);

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Developing a front end to SPLUNK 1 103
ModalPopup  question 22 56
.NET universe documentation poster 2 21
Delete command SQLdatasource in, vb 2 18
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from Go to that link and select download selenium in the right hand columnThat will then direct you to their download page.From that page s…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
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…
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.…

739 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