471,325 Members | 1,111 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,325 software developers and data experts.

Copying data between byte arrays

It's probably simple but I can't find a way how to copy number of bytes
from one byte array to another? Just like Array.Copy(SourceArray,
SourceIndex, DestArray, DestIndex, Length) does but in my case the
arrays have different length. Of course I can copy byte by byte but I
guess there should be a function for such task.
Thanks!

--
Vit Zayko
Dec 12 '05 #1
6 4478
Hi Vitaly,

take a look at "Buffer.BlockCopy"

Rainer
Dec 12 '05 #2
Using the same overload you've specified, it doesn't matter how long each
array is, as long as "DestArray" is at least "Length" in length, and the
difference between DestArray.Length and DestIndex is at least as long as
"Length."

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
You can lead a fish to a bicycle,
but you can't make it stink.

"Vitaly Zayko" <vitaly_at_zayko_dot_net> wrote in message
news:uZ**************@TK2MSFTNGP14.phx.gbl...
It's probably simple but I can't find a way how to copy number of bytes
from one byte array to another? Just like Array.Copy(SourceArray,
SourceIndex, DestArray, DestIndex, Length) does but in my case the arrays
have different length. Of course I can copy byte by byte but I guess there
should be a function for such task.
Thanks!

--
Vit Zayko

Dec 12 '05 #3
That's it! Thank you Rainer!
Hi Vitaly,

take a look at "Buffer.BlockCopy"

Rainer

--
Vit Zayko
Dec 12 '05 #4
The length of the arrays should not cause any problems to Array.Copy; e.g.
the following copies the second 50 bytes of a 100 byte array into (roughly)
the middle of a 500 byte arraym then prints some of the contents to validate
this.

byte[] source = new byte[100];
for (int i = 0; i < 100; i++)
source[i] = (byte) i;
byte[] dest = new byte[500];
Array.Copy(source, 50, dest, 200, 50); // source array, source
start-offset, destination array, destination start-offset, items to copy
for (int i = 195; i < 255; i++)
Console.WriteLine("{0}: {1}", i, dest[i]);

If you are going to copy the entire source array, source.CopyTo(dest,offset)
is an easier option - again, it doesn't need the arrays to be the same size,
as long as there is sufficient space in the destination.

Did I miss something in your question?

Marc

"Vitaly Zayko" <vitaly_at_zayko_dot_net> wrote in message
news:uZ**************@TK2MSFTNGP14.phx.gbl...
It's probably simple but I can't find a way how to copy number of bytes
from one byte array to another? Just like Array.Copy(SourceArray,
SourceIndex, DestArray, DestIndex, Length) does but in my case the arrays
have different length. Of course I can copy byte by byte but I guess there
should be a function for such task.
Thanks!

--
Vit Zayko

Dec 12 '05 #5
Well, my question wasn't complete, I'm sorry:
actually source array is string type, and I tried to use
String.ToCharArray(). Destination is byte array. When I tried to use
Copy I've got ArrayTypeMismatchException. That's why it didn't work
using Copy and it did using Buffer.BlockCopy.
Thanks to everyone!
Vit
Dec 12 '05 #6
<Vitaly Zayko <"vitaly at zayko dot net">> wrote:
Well, my question wasn't complete, I'm sorry:
actually source array is string type, and I tried to use
String.ToCharArray(). Destination is byte array. When I tried to use
Copy I've got ArrayTypeMismatchException. That's why it didn't work
using Copy and it did using Buffer.BlockCopy.


Hmm... that sounds like really you want Encoding.GetBytes() for the
appropriate encoding...

--
Jon Skeet - <sk***@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
Dec 12 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

21 posts views Thread by Matteo Settenvini | last post: by
5 posts views Thread by Roy Hills | last post: by
reply views Thread by Hannibal111111 | last post: by
4 posts views Thread by Jerry Spence1 | last post: by

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.