Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / December 2005

Tip: Looking for answers? Try searching our database.

How to do memcpy for Byte[]?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
EOS - 04 Dec 2005 01:13 GMT
I guess what I want to do is best explain via the codes here;

Byte[] byteA = new Byte[100000];
Byte[] byteB = new Byte[4];
...
...
...

Now I want to copy 4 bytes from certain portion of the byteA array.

In C/C++, this is what we do;

memcpy(byteB, byteA[X], 4);

Or

memcpy(byteB, byteA+X, 4);

But with C#, I had been stuck for a while with no luck....

Hope you understand what I wanted and could show me the way.

Thanks for your time. Sometime I wish Microsoft could create C# to be nearer
to C/C++. C# is still driving me headache...
balmerch - 04 Dec 2005 01:38 GMT
You could try:

public void CopyPartialByte(byte[] from, ref byte[] to, int index)
{
for (int x = index; x < (to.Length + index); x++)
   to[x - index] = from[x];
}

Then call it:
CopyPartialByte(byteA, ref byteB, index);

Like usual this is off the top of my head, might be an error in the loop
counting but you get the idea I hope.

Chris

> I guess what I want to do is best explain via the codes here;
>
[quoted text clipped - 20 lines]
> Thanks for your time. Sometime I wish Microsoft could create C# to be nearer
> to C/C++. C# is still driving me headache...
Daniel O'Connell [C# MVP] - 04 Dec 2005 02:45 GMT
> You could try:
>
[quoted text clipped - 3 lines]
>    to[x - index] = from[x];
> }

The ref shouldn't be needed here. An array is a reference type

You can also use Buffer.BlockCopy() to copy from arrays, look it up in MSDN
for the specifics.
EOS - 04 Dec 2005 09:08 GMT
Thanks! It is really easy to understand and to implement!

> You could try:
>
[quoted text clipped - 37 lines]
>> nearer
>> to C/C++. C# is still driving me headache...
Daniel Moth - 04 Dec 2005 01:40 GMT
See my reply to you in the PPC newsgroup

Cheers
Daniel
--
http://www.danielmoth.com/Blog/

>I guess what I want to do is best explain via the codes here;
>
[quoted text clipped - 20 lines]
> Thanks for your time. Sometime I wish Microsoft could create C# to be
> nearer to C/C++. C# is still driving me headache...
Jon Skeet [C# MVP] - 04 Dec 2005 07:16 GMT
> I guess what I want to do is best explain via the codes here;
>
> Byte[] byteA = new Byte[100000];
> Byte[] byteB = new Byte[4];

<snip>

See Buffer.BlockCopy, and Array.Copy.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.