473,412 Members | 2,284 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,412 software developers and data experts.

Structure as byte array

Hi

I am new to C# and am rewritting some C++ code. I want to send a byte array over a serial port. The elements of the byte array are really a structure I have populated. My question is, how do I do this in C# - that is, I want to declare a byte array, assign a structure to it, and then be able to populate the structure's fields, then use the byte array

Thank you
Joe
Nov 16 '05 #1
5 21584
Joe,

Part of this depends on what the structure is made up of. If the
structure completely contains its elements (no pointers, the arrays are
embedded within the structure), then a nice little trick you can do is to
create an array that is the size of the structure in bytes. Once you do
that, create an array of structures that contains one element. Set the
element in the array of structures to your structure, and then call the
static BlockCopy method on the Buffer class. This will copy the bytes in
the structure to the array.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Joe Thompson" <an*******@discussions.microsoft.com> wrote in message
news:23**********************************@microsof t.com...
Hi,

I am new to C# and am rewritting some C++ code. I want to send a byte array over a serial port. The elements of the byte array are really a
structure I have populated. My question is, how do I do this in C# - that
is, I want to declare a byte array, assign a structure to it, and then be
able to populate the structure's fields, then use the byte array.
Thank you,
Joe

Nov 16 '05 #2
Hi Nicholas

Thank you for the reply. My structure is self contained. I don't understand what you mean by
"call the static BlockCopy method on the Buffer class"
What is the Buffer class? I don't see a method called BlockCopy in C# anywhere..

Thank you
Joe
Nov 16 '05 #3
Joe,

The Buffer class is in the System namespace, and is in the mscorlib.dll
assembly, so it is available to you by default.

The only issue is knowing how large the structure is going to be. You
can determine this programatically by using the sizeof operator in unsafe
code. You might be tempted to use the static SizeOf method on the Marshal
class. I would recommend against that, because the size of the structure in
bytes on the unmanaged end is not always the same as the structure on the
managed end.

You can refer to the sizeof operator documentation to determine the size
of the structure:

http://msdn.microsoft.com/library/de...spec_A_5_8.asp

Your code should look like this:

// The structure we want to transform into bytes.
MyStruct pobjStruct = new MyStruct();

// Do some work to set the values. Now initialize the bytes.
// pintNumberOfBytesNeededForStructure is the number of bytes that the
structure takes up in memory.
byte[] pbytBytes = new byte[pintNumberOfBytesNeededForStructure];

// Create an array of the structures, make it one element long, and set to
your struct.
MyStruct[] pobjStructArray = new MyStruct[1];

// Set the value in the structure.
pobjStructAway[0] = pobjStruct;

// Copy the structure into the byte array.
Buffer.BlockCopy(pobjStructArray, 0, pbytBytes, 0,
pintNumberOfBytesNeededForStructure);
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Joe Thompson" <an*******@discussions.microsoft.com> wrote in message
news:AE**********************************@microsof t.com...
Hi Nicholas,

Thank you for the reply. My structure is self contained. I don't understand what you mean by: "call the static BlockCopy method on the Buffer class"
What is the Buffer class? I don't see a method called BlockCopy in C# anywhere...
Thank you,
Joe

Nov 16 '05 #4
Thanks Nicholas - that's what I was looking for..

Joe
Nov 16 '05 #5
Hi Nicholas

Maybe I replied to fast. I just ran the code and got the following runtime error on the Buffer.BlockCopy line

Object must be an array of primitive

Thank you
Joe
Nov 16 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Tien Pham via .NET 247 | last post by:
(Type your message here) -------------------------------- From: Tien Pham I am having problems with intptr. Or maybe i am not using it properly. What it is that i am having problem with is...
9
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
6
by: James | last post by:
I am using vb.net and need to keep in memory a large data structure, so I am looking for the best option. And after several test I am pretty confused. So I will be grateful if anyone can help me. ...
14
by: Dennis | last post by:
If I have a structure like; Public Structure myStructureDef Public b() as Byte Public t as String End Structure If I pass this structure, will the values in the array b be stored on the...
10
by: David Fort | last post by:
Hi, I'm upgrading a VB6 app to VB.net and I'm having a problem with a call to a function provided in a DLL. The function takes the address of a structure which it will fill in with values. I...
6
by: Aston Martin | last post by:
Hi All, ********************** My Situation ********************** I am working on project that involves passing a structure to unmanaged code from .Net world (well using C#). Perhaps an example...
5
by: =?Utf-8?B?QXlrdXQgRXJnaW4=?= | last post by:
Hi Willy, Thank you very much for your work. C++ code doesnot make any serialization. So at runtime C# code gives an serialization error at "msg_file_s sa = (msg_file_s) bf.Deserialize(ms);"...
6
by: carles | last post by:
Hi, Here, sample code where a byte array is used to fill a particular structure: fs = File.OpenRead(path); // FileStream BITMAPFILEHEADER bfh = new BITMAPFILEHEADER(); b = new byte;
10
by: Scott Townsend | last post by:
So I need to talk to a devices that expects all of the bits and bytes I sent it to be in specific places (not yet 100% defined). I wanted to create a structure/class with all of the data in it...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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
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...
0
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...

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.