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# / December 2005

Tip: Looking for answers? Try searching our database.

Copying data between byte arrays

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Vitaly Zayko - 12 Dec 2005 14:42 GMT
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!

Signature

Vit Zayko

Rainer Queck - 12 Dec 2005 14:55 GMT
Hi Vitaly,

take a look at "Buffer.BlockCopy"

Rainer
Vitaly Zayko - 12 Dec 2005 15:06 GMT
That's it! Thank you Rainer!

> Hi Vitaly,
>
> take a look at "Buffer.BlockCopy"
>
> Rainer

Signature

Vit Zayko

Kevin Spencer - 12 Dec 2005 15:01 GMT
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."

Signature

HTH,

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

> 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!
Marc Gravell - 12 Dec 2005 15:04 GMT
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

> 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!
Vitaly Zayko - 12 Dec 2005 17:06 GMT
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
Jon Skeet [C# MVP] - 12 Dec 2005 19:08 GMT
> 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...

Signature

Jon Skeet - <skeet@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


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.