473,688 Members | 3,202 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 StatisticsRepor t
{
public int a;
public int b;

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

What is the best way to convert StatisticsRepor t 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 3757
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 StatisticsRepor t
{
* public int a;
* public int b;

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

}

What is the best way to convert StatisticsRepor t 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.

StatisticsRepor t originalStruct;
StatisticsRepor t destStruct;

//to array
byte[] arr = new byte[sizeof(Struct)];
Marshal.Copy((I ntPtr)(byte*)&o riginalStruct, arr, 0, arr.Length);

//from array
Marshal.Copy(ar r, 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
11039
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 an integer - now I know that in C I'd just cast a pointer to the offset to a short, and that python doesn't cast, so how can I extract the value from a stream of bytes. I've looked at python.org/Doc/current (I'm using 2.3b1), but can't find...
7
52461
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 array.. Thanks & Regards Prakash Prabhu
2
12188
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
5322
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 40-56 and create struct Foo from them...how do I do this? You can assume I know how to create the properly aligned struct. Thanks Amil
9
12685
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
6256
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
4459
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 get a "unspecified error" if I tried to add the data before converting to a byte in "deNewContextObject.Properties.Add((object)blob.pData);". public struct Blob { public IntPtr pData; public int nLength;
7
7235
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
22232
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 = open("test2.pc0", "rb") In : tagData = f.read(4) In : tagData
0
8590
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8528
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9072
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8786
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6453
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4547
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2964
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 we have to send another system
2
2215
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1950
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.