473,320 Members | 2,027 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,320 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 4640
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
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...
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...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
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: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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.