Copy byte array to other

G

Guest

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++.
 
N

Nicholas Paldino [.NET/C# MVP]

SushiSean,

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

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

Ignacio Machin \( .NET/ C# MVP \)

SushiSean said:
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.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Sorry, I sent the first post before finishing it.


SushiSean said:
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.
 
P

Peter Duniho

Nicholas said:
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
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

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.
 
J

Justin Chase

Additionally you could create a structure that matches the size of each array
and use Marshal.PtrToStructure and Marshal.StructureToPtr to convert the
byte[]'s into actual structs. At that point you could just set fields or
properties on each object.

Maybe a little more work but it could, potentially, increase the
maintainability over time.
 
J

Jeff Johnson

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++.

System.Buffer.BlockCopy()
 
B

Ben Voigt [C++ MVP]

Jeff said:
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++.

System.Buffer.BlockCopy()

or just Array.Copy
 
J

Jeff Johnson

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++.

System.Buffer.BlockCopy()

or just Array.Copy

Silly me. I figured if such as thing existed it would be the first thing
ANYONE would look for, so I took the question as evidence that it didn't
exist. I should have learned that lesson years ago!

Shows you how much I use the basic array methods, huh? (Actually, I've
probably used CopyTo far more than Copy.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top