473,398 Members | 2,393 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,398 software developers and data experts.

BlockCopy

From a socket, I read some data into a bytes buffer.

byte[] bytes = new byte[1024];
int headSize = 8;
int nRead = sock.Receive(bytes, 0, headSize, SocketFlags.Peek);

Next I would like to 'mask' the received data with a struct 'HeaderT'

public struct HeaderT
{
int packetSize;
int type;
};

In C++ this could be done similar to this:
HeaderT *pHead = (HeaderT *)bytes;

In C#, this obviously does not work, so I tried with BlockCopy():

HeaderT header = new HeaderT();
System.Buffer.BlockCopy(bytes, 0, header , 0, headSize);

But then I get the following syntax error
Argument '3': cannot convert from 'HeaderT' to 'System.Array'.

So far I found no solution. Any help would be appreciated
Regards Urs

Nov 15 '05 #1
2 4112
Urs,

You will have to pass an array of type HeaderT into the function, with
one element in it.

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

"Urs Wigger" <ns_urs.wigger@leica-geosystems_ns.com> wrote in message
news:Od*************@TK2MSFTNGP09.phx.gbl...
From a socket, I read some data into a bytes buffer.

byte[] bytes = new byte[1024];
int headSize = 8;
int nRead = sock.Receive(bytes, 0, headSize, SocketFlags.Peek);

Next I would like to 'mask' the received data with a struct 'HeaderT'

public struct HeaderT
{
int packetSize;
int type;
};

In C++ this could be done similar to this:
HeaderT *pHead = (HeaderT *)bytes;

In C#, this obviously does not work, so I tried with BlockCopy():

HeaderT header = new HeaderT();
System.Buffer.BlockCopy(bytes, 0, header , 0, headSize);

But then I get the following syntax error
Argument '3': cannot convert from 'HeaderT' to 'System.Array'.

So far I found no solution. Any help would be appreciated
Regards Urs

Nov 15 '05 #2
You will have to pass an array of type HeaderT into the function, with
one element in it.


I don't think BlockCopy works with UDTs, only with the primitive
types. So I'd do something like this instead

HeaderT header;
header.packetSize = BitConverter.ToInt32( bytes, 0 );
header.type = BitConverter.ToInt32( bytes, 4 );

In C++ this could be done similar to this:
HeaderT *pHead = (HeaderT *)bytes;

In C#, this obviously does not work


Actually you can, in unsafe code. But you may not want to do that.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
Nov 15 '05 #3

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

Similar topics

4
by: Marius Cabas | last post by:
I have a Byte array and I want to concatenate it to another Byte array. How can I do this? Thanks.
8
by: Tee | last post by:
Hi, How do we increase the size of array on runtime and preserve the previous data? I don't want to use ArrayList because the array could be a multi-dimension array. Thanks, Tee
0
by: William Stacey [MVP] | last post by:
This code worked on fx 1.1 and now I get a "Keyset does not exist" error when trying to SignData with RSA under FX2.0. Smells like some security error, but can't debug it as I think error is...
5
by: John Koisch | last post by:
All, In vb6.0, some of our co-developers used the following ... Private Declare Sub MoveMemory Lib "kernel32.dll" Alias "RtlMoveMemory" (ByVal dst As Any, ByVal src As Any, ByVal cb As Long)...
0
by: Karch | last post by:
I am trying to marshal some binary data in my code before passing it to a SQL Service Broker queue. I found this code for marshaling the binary data, but I would like to also do this in my client...
0
by: JonJacobs | last post by:
When I add a series of byte arrays to an array list, then I read them back, all the arraylist byte array elements are identical to the last byte array entry. What is wrong? The following code will...
1
by: kellox | last post by:
Does anybody know the difference between the two static methods Buffer.BlockCopy and Array.Copy? It is said that Buffer.BlockCopy is faster than Array.Copy since it only checks boundary and then...
6
by: =?Utf-8?B?U3VzaGlTZWFu?= | last post by:
Hello. How I can copy one byte array to other byte array? For example I have byte array1 = new byte; byte array2 = new byte; in array1 I have useful data from position 55 to 105 and I need...
3
by: click2ranjan | last post by:
Hi friend i am new in network programming and trying to develop a chat messenger. for this most of the portion has completed but still i have some problem with file sending to anothe user, files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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
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,...

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.