?
Solved

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

Posted on 2008-06-12
6
Medium Priority
?
382 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
[X]
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
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 400 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The viewer will learn how to use and create new code templates 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…

800 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