473,224 Members | 1,588 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,224 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 4631
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: Bryan Rickard | last post by:
I wrote a simple program in VB6 to copy all the files from a directory on a CD-ROM to my hard disk. There are about 10 files, each about 30MB. The program uses Get and Put to get data from the...
21
by: Matteo Settenvini | last post by:
Ok, I'm quite a newbie, so this question may appear silly. I'm using g++ 3.3.x. I had been taught that an array isn't a lot different from a pointer (in fact you can use the pointer arithmetics to...
5
by: Roy Hills | last post by:
When I'm reading from or writing to a network socket, I want to use a struct to represent the structured data, but must use an unsigned char buffer for the call to sendto() or recvfrom(). I have...
1
by: jeric | last post by:
I have two byte arrays, X and Y. X contains 100 elements, and Y has 10 elements. I want Y to equal X through X. How can I do this without a loop? While in this example the arrays are only...
6
by: Max Metral | last post by:
I have a "high performance" application I'm building in C#. I have a preparation program that builds a large file that is layed out "properly" with indexes etc. So my goal is to read that file...
0
by: Brian Black | last post by:
Hi, Sorry For the Cross Posting But, I just started programming .net and i have a question. I have a byte array of 14 bytes (CODE BELOW), i need to copy the data from the the byte array into...
0
by: Hannibal111111 | last post by:
I found this code on a site for doing string encryption/decryption. The string will encrypt fine, but I get this error when I try to decrypt. Any idea why? I posted the code below. The error...
4
by: Jerry Spence1 | last post by:
I have one array which is base 1 (rather than 0) and I am trying to use array.copy to copy it into a new array. I get the error: Unable to cast object of type 'System.Byte' to type 'System.Byte'....
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...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

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.