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 / Languages / C# / October 2007

Tip: Looking for answers? Try searching our database.

Copy byte array to other

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
SushiSean - 09 Oct 2007 17:39 GMT
Hello. How I can copy one byte array to other byte array?

For example I have
byte[] array1 = new byte[500];
byte[] array2 = new byte[100];

in array1  I have useful data from position 55 to 105 and I need copy those
bytes to array2 on position from 15 to 65.
How to do this? There are should be some function which work like "copy"
function in C++.
Nicholas Paldino [.NET/C# MVP] - 09 Oct 2007 17:50 GMT
SushiSean,

   You would use the static BlockCopy method on the Buffer class, like so:

Buffer.BlockCopy(array1, 55, 51, array2, 15, 51);

Signature

         - Nicholas Paldino [.NET/C# MVP]
         - mvp@spam.guard.caspershouse.com

> Hello. How I can copy one byte array to other byte array?
>
[quoted text clipped - 7 lines]
> How to do this? There are should be some function which work like "copy"
> function in C++.
Peter Duniho - 09 Oct 2007 18:35 GMT
> SushiSean,
>
>     You would use the static BlockCopy method on the Buffer class, like so:
>
> Buffer.BlockCopy(array1, 55, 51, array2, 15, 51);

Not to dispute the usefulness of Buffer.BlockCopy(), but it seems to me
that in this case, where one is simply copying from one array to
another, the Array.Copy() method would be preferable (if nothing else,
just from a readability point of view):

    Array.Copy(array1, 55, array2, 15, 51);

Also, I think that if one is using Buffer.BlockCopy(), the correct
parameter list is this (basically the same as Array.Copy()):

    Buffer.BlockCopy(array1, 55, array2, 15, 51);

Any particular reason for preferring Buffer.BlockCopy()?  I prefer
Array.Copy() because it's a method that already exists in the class for
the data type being used (even if it is a static method).  Is there
something about BlockCopy() that makes it better?

Pete
Ignacio Machin ( .NET/ C# MVP ) - 09 Oct 2007 19:01 GMT
Hi,

>> SushiSean,

> Any particular reason for preferring Buffer.BlockCopy()?  I prefer
> Array.Copy() because it's a method that already exists in the class for
> the data type being used (even if it is a static method).  Is there
> something about BlockCopy() that makes it better?

I think I read somewhere (or maybe here in an older post) that BlockCopy
simply use an equivalent of memcpy, on other words it just copy a chunk of
bytes.
Array.Copy takes into account the type of the array (to deal with different
sizes in memory). Basically Array.Copy you go by index, wheater in BlockCopy
is just a byte copying method.

So BlockCopy should be faster.

Disclaimer:
The above is from memory, I haven't check the docs nor the dissassambled
code to check it.
Ignacio Machin ( .NET/ C# MVP ) - 09 Oct 2007 18:03 GMT
> Hello. How I can copy one byte array to other byte array?
>
[quoted text clipped - 7 lines]
> How to do this? There are should be some function which work like "copy"
> function in C++.

Buffer.BlockCopy will do it.
Ignacio Machin ( .NET/ C# MVP ) - 09 Oct 2007 18:04 GMT
Sorry, I sent the first post before finishing it.

> Hello. How I can copy one byte array to other byte array?
>
[quoted text clipped - 7 lines]
> How to do this? There are should be some function which work like "copy"
> function in C++.

Also take a look at the several Array.Copy methods.
Matt Brunell - 09 Oct 2007 19:12 GMT
You can use the static method "Copy" on the Array class.

Like this:

Array.Copy(source,55,dest,15,50);

See http://msdn2.microsoft.com/en-us/library/system.array.copy.aspx

> Hello. How I can copy one byte array to other byte array?
>
[quoted text clipped - 7 lines]
> How to do this? There are should be some function which work like "copy"
> function in C++.

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.