C# 程式從一個數組複製一系列的位元組到另一個數組


使用 Buffer.BlockCopy 方法從一個數組複製一系列的位元組到另一個數組 -

設定一個位元組陣列 -

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

從一個數組複製位元組到另一個數組 -

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

以下是完整的程式碼 -

示例

 線上演示

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();
   }
}

輸出

2249000

更新時間: 22-Jun-2020

3K+ 瀏覽量

開啟你的 事業

完成課程獲得認證

開始
廣告