469,315 Members | 2,130 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,315 developers. It's quick & easy.

Copy byte array to other

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++.
Oct 9 '07 #1
6 71178
SushiSean,

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

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

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

"SushiSean" <Su*******@discussions.microsoft.comwrote in message
news:C2**********************************@microsof t.com...
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++.

Oct 9 '07 #2
Sorry, I sent the first post before finishing it.
"SushiSean" <Su*******@discussions.microsoft.comwrote in message
news:C2**********************************@microsof t.com...
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++.
Also take a look at the several Array.Copy methods.
Oct 9 '07 #3

"SushiSean" <Su*******@discussions.microsoft.comwrote in message
news:C2**********************************@microsof t.com...
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++.
Buffer.BlockCopy will do it.
Oct 9 '07 #4
Nicholas Paldino [.NET/C# MVP] wrote:
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
Oct 9 '07 #5
Hi,

"Peter Duniho" <Np*********@NnOwSlPiAnMk.comwrote in message
news:13*************@corp.supernews.com...
Nicholas Paldino [.NET/C# MVP] wrote:
>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.
Oct 9 '07 #6
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/lib...rray.copy.aspx
"SushiSean" <Su*******@discussions.microsoft.comwrote in message
news:C2**********************************@microsof t.com...
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++.
Oct 9 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Sathyaish | last post: by
4 posts views Thread by Lance | last post: by
2 posts views Thread by TonyJ | last post: by
10 posts views Thread by Scott Townsend | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.