473,898 Members | 2,481 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 72918
SushiSean,

You would use the static BlockCopy method on the Buffer class, like so:

Buffer.BlockCop y(array1, 55, 51, array2, 15, 51);

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"SushiSean" <Su*******@disc ussions.microso ft.comwrote in message
news:C2******** *************** ***********@mic rosoft.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*******@disc ussions.microso ft.comwrote in message
news:C2******** *************** ***********@mic rosoft.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*******@disc ussions.microso ft.comwrote in message
news:C2******** *************** ***********@mic rosoft.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.BlockCop y 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.BlockCop y(array1, 55, 51, array2, 15, 51);
Not to dispute the usefulness of Buffer.BlockCop y(), 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(arra y1, 55, array2, 15, 51);

Also, I think that if one is using Buffer.BlockCop y(), the correct
parameter list is this (basically the same as Array.Copy()):

Buffer.BlockCop y(array1, 55, array2, 15, 51);

Any particular reason for preferring Buffer.BlockCop y()? 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*********@Nn OwSlPiAnMk.comw rote in message
news:13******** *****@corp.supe rnews.com...
Nicholas Paldino [.NET/C# MVP] wrote:
>SushiSean,
>
Any particular reason for preferring Buffer.BlockCop y()? 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(sour ce,55,dest,15,5 0);

See http://msdn2.microsoft.com/en-us/lib...rray.copy.aspx
"SushiSean" <Su*******@disc ussions.microso ft.comwrote in message
news:C2******** *************** ***********@mic rosoft.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
2730
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 in the byte array gets copied to 1 element in the Int16 array. In VB6, this was easily done using CopyMemory. I need something in .NET that works as fast as CopyMemory. I
19
4172
by: jeff | last post by:
how do you convert form byte to Int32 while retaining the binary value of the byte array
2
4361
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 this sound (file) on a Windows socket (not using FTP but TCP), can I read the file into a byte array? Because if it is possible then I can send it on the socket but the problems I foresee are: How will I repack it into the file. Will just reading...
16
14171
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 byteArray and afterwards see the changes in C#. (if you wanna know more details: the goal is to write the content of an existing char-array in c++ precisely into the passed byteArray, so that after the function has proceded the content of the...
5
9872
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 the memory across. Using Marshal.PtrStructure doesn't work - it says my byte() array is not blittable! (byte is a blittable type however). Cannot use Marshal.Copy, because that works the other way around (for mashalling to COM, not from it). ...
4
2949
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 32). For example, lets say I have a an array of bytes (byteValues) that represents a string of 7 bit values. In this case, the first value would be stored in the first 7 bits of byteValues(0), the second value would be stored in the last bit of...
2
12741
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 without having to use a temporary byte array as I have done below? //byte buffer = new byte;
10
6397
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 and then convert that to a byte array, pass it to the device, then get a reply and then convert that to a structure. I'm having issues with making sure what I've coded is correct. Cant figure out how to define an array in structure that is a...
2
7216
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 Marshal.PtrToStructure to copy the all the data into the "other" array? unsafe public struct DeadReckoning {
0
9993
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10859
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10954
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10487
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
8036
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7191
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5882
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4708
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3308
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.