Computer >> คอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#

C# BitConverter.ToInt64() Method


วิธีการ BitConverter.ToInt64() ใน C # ใช้เพื่อส่งคืนจำนวนเต็มที่ลงนาม 64 บิตซึ่งแปลงจากแปดไบต์ที่ตำแหน่งที่ระบุในอาร์เรย์ไบต์

ไวยากรณ์

ไวยากรณ์มีดังนี้ −

public static long ToInt64 (byte[] val, int begnIndex);

ด้านบน val คืออาร์เรย์ไบต์ ในขณะที่ begnIndex เป็นตำแหน่งเริ่มต้นภายใน val

เรามาดูตัวอย่างกัน −

ตัวอย่าง

using System;
public class Demo {
   public static void Main() {
      byte[] arr = { 0, 10, 15, 20, 26, 30, 34, 42, 50};
      Console.WriteLine("Byte Array = {0} ", BitConverter.ToString(arr));
      for (int i = 0; i < arr.Length - 7; i = i + 8) {
         long res = BitConverter.ToInt64(arr, i);
         Console.WriteLine("\nValue = "+arr[i]);
         Console.WriteLine("Result = "+res);
      }
   }
}

ผลลัพธ์

สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้ -

Byte Array = 00-0A-0F-14-1A-1E-22-2A-32
Value = 0
Result = 3036022196155648512

ตัวอย่าง

เรามาดูตัวอย่างอื่นกัน −

using System;
public class Demo {
   public static void Main() {
      byte[] arr = {0, 0, 0, 10, 20, 0, 0, 25, 30, 0, 0, 0, 35, 45, 55, 65, 75};
      Console.WriteLine("Byte Array = {0} ",
      BitConverter.ToString(arr));
      for (int i = 0; i < arr.Length - 7; i = i + 8) {
         long res = BitConverter.ToInt64(arr, i);
         Console.WriteLine("\nValue = "+arr[i]);
         Console.WriteLine("Result = "+res);
      }
   }
}

ผลลัพธ์

สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้ -

Byte Array = 00-00-00-0A-14-00-00-19-1E-00-00-00-23-2D-37-41-4B
Value = 0
Result = 1801439937015316480
Value = 30
Result = 4699274364531507230