convert struct to byte[]

P

Paul Jarvis

I have a large structure, below is a simplistic version of my structure:

public struct MeanMinMaxSd
{
public double mean;
public double min;
public double max;
public double sd;
}

public struct StatisticsReport
{
public int a;
public int b;

public MinMax c;
public MinMax d;
public Minmax e;
}

What is the best way to convert StatisticsReport to a byte[] and back again?

Thanks

Paul Jarvis
PhD Student
The School of Community Based Medicine
The University of Manchester
 
N

Nicholas Paldino [.NET/C# MVP]

Paul,

To what end do you need to do this? I ask because what you are going to
use that byte array for is important.

Are you going to pass it to unmanaged memory for an API in a native dll?
In that case, you will have to use the static StructureToPtr method to
marshal it to unmanaged memory, and if you want it in a managed byte array,
you will have to copy that block of unmanaged memory back to managed memory.

If you are looking to persist the structure to some storage, and then
get that information back somewhere else, you can use serialization.

The format that you are expecting is important here. If you clarify, it
would help tremendously. =)
 
F

forever.zet

I have a large structure, below is a simplistic version of my structure:

public struct MeanMinMaxSd
{
  public double mean;
  public double min;
  public double max;
  public double sd;

}

public struct StatisticsReport
{
  public int a;
  public int b;

  public MinMax c;
  public MinMax d;
  public Minmax e;

}

What is the best way to convert StatisticsReport to a byte[] and back again?

Thanks

Paul Jarvis
PhD Student
The School of Community Based Medicine
The University of Manchester

--
--------------------------------- --- -- -
Posted with NewsLeecher v3.9 Beta 11
Web @http://www.newsleecher.com/?usenet
------------------- ----- ---- -- -

I know the only way using unsafe code. However there are too few cases
when
you really need this. Usually there are ways to go without it.

StatisticsReport originalStruct;
StatisticsReport destStruct;

//to array
byte[] arr = new byte[sizeof(Struct)];
Marshal.Copy((IntPtr)(byte*)&originalStruct, arr, 0, arr.Length);

//from array
Marshal.Copy(arr, 0, (IntPtr)(byte*)&destStruct, arr.Length);

Thanks,
Sergey Zyuzin
 
M

Marc Gravell

Not related to the question - but a quick note: in .NET it is actually
very rare to want to declare a "struct", and even rarer to want to
declare a mutable struct (i.e. where you can update the members at
some point).

I'm not saying that it is necessarily wrong - I'm just trying to save
you some pain down-stream. I just think you should double-check that
you really understand and want value-type semantics here. A class
would be more typical, especially since you say it is large (that poor
stack...); normally when I see "struct" it is a hangover from C++
days, but it means something very, very different.

Oh, and also: public fields are very uncommon too ;-p

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top