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

เมธอด BitConverter.ToUInt64() ใน C# พร้อมตัวอย่าง


เมธอด BitConverter.ToUInt64() ใน C# ใช้สำหรับแปลง 8 ไบต์จากตำแหน่งที่ระบุใน Byte Array ให้เป็น Unsigned 64-bit Integer (ulong)

Syntax

public static ulong ToUInt64 (byte[] val, int begnIndex);

ตัวอย่าง

using System; 
public class Demo { 
    public static void Main() { 
        byte[] arr = { 0, 0, 1, 3, 5, 7, 9, 11, 15 }; 
        int count = arr.Length; 
        Console.Write("Byte Array... "); 
        for (int i = 0; i < count; i++) { 
            Console.Write("\n" + arr[i]); 
        } 
        Console.WriteLine("\n\nByte Array (String representation) = {0} ", BitConverter.ToString(arr)); 
        for (int i = 1; i < arr.Length - 7; i = i + 8) { 
            ulong res = BitConverter.ToUInt64(arr, i); 
            Console.WriteLine("\nValue = " + arr[i]); 
            Console.WriteLine("Result = " + res); 
        } 
    } 
}

ผลลัพธ์

Byte Array... 
0 
0 
1 
3 
5 
7 
9 
11 
15 
Byte Array (String representation) = 00-00-01-03-05-07-09-0B-0F 
Value = 0 
Result = 1083970061066240256

ตัวอย่างเพิ่มเติม

using System; 
public class Demo { 
    public static void Main() { 
        byte[] arr = { 0, 0, 1, 3, 5, 7, 9, 11, 15, 0, 1, 6, 8, 10, 20, 25, 36, 34 }; 
        int count = arr.Length; 
        Console.Write("Byte Array... "); 
        for (int i = 0; i < count; i++) { 
            Console.Write("\n" + arr[i]); 
        } 
        Console.WriteLine("\n\nByte Array (String representation) = {0} ", BitConverter.ToString(arr)); 
        for (int i = 1; i < arr.Length - 7; i = i + 8) { 
            ulong res = BitConverter.ToUInt64(arr, i); 
            Console.WriteLine("\nValue = " + arr[i]); 
            Console.WriteLine("Result = " + res); 
        } 
    } 
}

ผลลัพธ์

Byte Array... 
0 
0 
1 
3 
5 
7 
9 
11 
15 
0 
1 
6 
8 
10 
20 
25 
36 
34 
Byte Array (String representation) = 00-00-01-03-05-07-09-0B-0F-00-01-06-08-0A-14-19-24-22 
Value = 0 
Result = 1083970061066240256 
Value = 0 
Result = 2601132293100011776