473,508 Members | 2,396 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3684
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
11026
by: Graham Nicholls | last post by:
Hi, I'm trying to size a jpeg file. The file size is held in a short (2 byte integer) at a certain offset. Once I've found these two bytes (they're in MSB,LSB order), I need to convert them to...
7
52418
by: Prabhu | last post by:
Hi, I have to send a structure through TCPClient socket. we can send only byte array through the socket, So please any one can help me by telling How to convert a struct object into an byte...
2
12155
by: Mel WEaver | last post by:
Hello, I have the following delphi structure for a binary file. I'm looking for idea how to read this file. Mel type TMenuDataStruct = packed record exename : string;
5
5308
by: Amil Hanish | last post by:
I have a huge byte array from another API that contains structs of differing sizes. I'd like to create n structs based on data in the byte array. But how? For example, if I want to take bytes...
9
12664
by: Charles Law | last post by:
Suppose I have a structure Private Structure MyStruct Dim el1 As Byte Dim el2 As Int16 Dim el3 As Byte End Structure I want to convert this into a byte array where
7
6231
by: shellon | last post by:
Hi all: I want to convert the float number to sortable integer, like the function float2rawInt() in java, but I don't know the internal expression of float, appreciate your help!
6
4450
by: =?Utf-8?B?UHVjY2E=?= | last post by:
Hi, I'm using VS2005, .net 2 and C# for windows application. I need to convert a IntPtr to a byte to be able to add a meetingBlob data to the meeting class object in Active Directory schema. I...
7
7176
by: Mario M. Mueller | last post by:
Hi, I have a binary file containing 3 byte float values (big endian). How can I read them into python? The struct module does not work, since it expects 4 byte floats. Any hints? Mario
4
22100
by: Mason | last post by:
I have tried and tried... I'd like to read in a binary file, convert it's 4 byte values into floats, and then save as a .txt file. This works from the command line (import struct); In : f =...
0
7224
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7118
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7493
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5625
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4706
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3192
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3180
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1550
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
415
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.