C# program to copy a range of bytes from one array to another



Use the Buffer.BlockCopy method to copy a range of bytes from one array to another −

Set a byte array −

byte[] b1 = new byte[] {22, 49}; byte[] b2 = new byte[5];

Copy bytes from one array to another −

Buffer.BlockCopy(b1, 0, b2, 0, 2);

The following is the complete code −

Example

 Live Demo

using System; class Demo {    static void Main(){       // byte arrays       byte[] b1 = new byte[] {22, 49};       byte[] b2 = new byte[5];       // copying bytes from one to another       Buffer.BlockCopy(b1, 0, b2, 0, 2);       /* calling the method with the byte array b2 that has the copied elements */       bufferFunc(b2);    }    static void bufferFunc(byte[] a) {       for (int j = 0; j < a.Length; j++) {          Console.Write(a[j]);       }       Console.WriteLine();    } }

Output

2249000
Updated on: 2020-06-22T14:01:56+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements