469,568 Members | 1,598 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,568 developers. It's quick & easy.

convert struct to byte[]

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

Jan 21 '08 #1
1 3299
On Jan 21, 5:09*pm, Paul Jarvis <pjar...@man.ac.ukwrote:
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
Jan 21 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Graham Nicholls | last post: by
2 posts views Thread by Mel WEaver | last post: by
5 posts views Thread by Amil Hanish | last post: by
9 posts views Thread by Charles Law | last post: by
6 posts views Thread by =?Utf-8?B?UHVjY2E=?= | last post: by
7 posts views Thread by Mario M. Mueller | last post: by
4 posts views Thread by Mason | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.