473,322 Members | 1,287 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,322 software developers and data experts.

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 72815
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: TDH | last post by:
Hi, I need to do a raw copy of a byte array to an Int16 array. For e.g., I have an byte array which has length 100, and I need to copy this to an Int16 array that has length 50. So 2 elements...
19
by: jeff | last post by:
how do you convert form byte to Int32 while retaining the binary value of the byte array
2
by: Sathyaish | last post by:
I am using MCI (winmm.dll) to read, record and playback sound. For now, I am doing this with disk files instead of realtime doing it straight from the memory. If I want to stream/relay/transmit...
16
by: Ekim | last post by:
hello, I'm allocating a byte-Array in C# with byte byteArray = new byte; Now I want to pass this byte-Array to a managed C++-function by reference, so that I'm able to change the content of the...
5
by: Robin Tucker | last post by:
I need to marshal an IntPtr (which I've got from GlobalLock of an HGLOBAL) into a byte array. I know the size of the array required and I've got a pointer to the blob, but I can't see how to copy...
4
by: Lance | last post by:
I have an array of bytes that I need to convert into an array of Integers. But, the number of bits per value in the Byte array is not necessarily divisible by 8 (although it will never exceed...
2
by: TonyJ | last post by:
Hello! Now to my question. Here I copy from a byte array into another byte array and then convert to a string. Is it possible to convert some part from a byte array into a string directly...
10
by: Scott Townsend | last post by:
So I need to talk to a devices that expects all of the bits and bytes I sent it to be in specific places (not yet 100% defined). I wanted to create a structure/class with all of the data in it...
2
by: O.B. | last post by:
When using Marshal to copy data from a byte array to the structure below, only the first byte of the "other" array is getting copied from the original byte array. What do I need to specify to get...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.