Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> C#

Phương thức BitConverter.ToUInt16() trong C#

Giới thiệu

Phương thức BitConverter.ToUInt16(byte[] value, int startIndex) chuyển đổi 2 byte từ mảng byte (bắt đầu tại startIndex) thành số nguyên 16-bit không dấu (ushort, range 0..65535). Byte order phụ thuộc vào endianness của hệ thống (thường little-endian trên x86/x64).

Cú pháp

public static ushort ToUInt16(byte[] value, int startIndex);

Ví dụ 1

using System;

public class Demo
{
    public static void Main()
    {
        byte[] arr = { 10, 20, 30, 40, 50 };
        int count = arr.Length;

        Console.Write("Mảng byte... ");
        for (int i = 0; i < count; i++)
        {
            Console.Write("\n" + arr[i]);
        }

        Console.WriteLine("\n\nMảng byte (dạng hex string) = {0}", BitConverter.ToString(arr));

        // Đọc ushort từ các cặp byte (little-endian: byte thấp trước)
        for (int i = 1; i < arr.Length - 1; i = i + 2)
        {
            ushort res = BitConverter.ToUInt16(arr, i);
            Console.WriteLine("\nGiá trị byte[{0}] = {1}", i, arr[i]);
            Console.WriteLine("Kết quả ToUInt16 = " + res);
        }
    }
}

Kết quả

Mảng byte...
10
20
30
40
50

Mảng byte (dạng hex string) = 0A-14-1E-28-32
Giá trị byte[1] = 20
Kết quả ToUInt16 = 7700
Giá trị byte[3] = 40
Kết quả ToUInt16 = 12840

Giải thích little-endian: ToUInt16(arr, 1) đọc byte[1]=20 (low byte) và byte[2]=30 (high byte) => 0x1E14 = 7700.

Ví dụ 2

using System;

public class Demo
{
    public static void Main()
    {
        byte[] arr = { 0, 0, 1, 3, 5, 7, 10, 16, 20, 34, 42, 55, 66, 75 };
        int count = arr.Length;

        Console.Write("Mảng byte... ");
        for (int i = 0; i < count; i++)
        {
            Console.Write("\n" + arr[i]);
        }

        Console.WriteLine("\n\nMảng byte (dạng hex string) = {0}", BitConverter.ToString(arr));

        for (int i = 1; i < arr.Length - 1; i = i + 2)
        {
            ushort res = BitConverter.ToUInt16(arr, i);
            Console.WriteLine("\nGiá trị byte[{0}] = {1}", i, arr[i]);
            Console.WriteLine("Kết quả ToUInt16 = " + res);
        }
    }
}

Kết quả

Mảng byte...
0
0
1
3
5
7
10
16
20
34
42
55
66
75

Mảng byte (dạng hex string) = 00-00-01-03-05-07-0A-10-14-22-2A-37-42-4B
Giá trị byte[1] = 0
Kết quả ToUInt16 = 256
Giá trị byte[3] = 3
Kết quả ToUInt16 = 1283
Giá trị byte[5] = 7
Kết quả ToUInt16 = 2567
Giá trị byte[7] = 16
Kết quả ToUInt16 = 5136
Giá trị byte[9] = 34
Kết quả ToUInt16 = 10786
Giá trị byte[11] = 55
Kết quả ToUInt16 = 16951

Lưu ý quan trọng

  • Endianness: Kết quả khác nhau trên little-endian (Intel/AMD) và big-endian. Kiểm tra BitConverter.IsLittleEndian.
  • Ném ArgumentException nếu startIndex vượt phạm vi (cần ít nhất 2 byte từ startIndex).
  • Ném ArgumentNullException nếu mảng là null.
  • Để code portable cross-platform, dùng BinaryPrimitives.ReadUInt16LittleEndian/BigEndian (.NET Core 2.1+).
  • ToString(byte[]) rất tiện để debug: xuất hex với dấu gạch nối.