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

เมธอด BitConverter.ToUInt16() ใน C#

เมธอด BitConverter.ToUInt16() ใน C# ใช้แปลง 2 ไบต์ (16 bits) จากอาเรย์ไบต์ที่ตำแหน่งที่ระบุ ให้เป็น 16-bit unsigned integer (ushort)

ไวยากรณ์ (Syntax)

public static ushort ToUInt16(byte[] value, int startIndex);
  • value — อาเรย์ไบต์ต้นทาง
  • startIndex — ตำแหน่งเริ่มต้นในอาเรย์ (zero-based) — ต้องมีอย่างน้อย 2 ไบต์เหลืออยู่
  • คืนค่า ushort (0 ถึง 65,535)
  • โยน ArgumentException หาก startIndex ไม่ถูกต้อง หรือ ArgumentNullException หาก value เป็น null

ตัวอย่างที่ 1: แปลงไบต์คู่ๆ ในอาเรย์

using System;

public class Demo
{
    public static void Main()
    {
        byte[] arr = { 10, 20, 30, 40, 50 };
        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 - 1; i = i + 2)
        {
            ushort res = BitConverter.ToUInt16(arr, i);
            Console.WriteLine("\nValue = " + arr[i]);
            Console.WriteLine("Result = " + res);
        }
    }
}

ผลลัพธ์

Byte Array...
10
20
30
40
50
Byte Array (String representation) = 0A-14-1E-28-32
Value = 20
Result = 7700
Value = 40
Result = 12840

ตัวอย่างที่ 2: อาเรย์ยาวกว่า พร้อมค่า Fibonacci-ish

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("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 - 1; i = i + 2)
        {
            ushort res = BitConverter.ToUInt16(arr, i);
            Console.WriteLine("\nValue = " + arr[i]);
            Console.WriteLine("Result = " + res);
        }
    }
}

ผลลัพธ์

Byte Array...
0
0
1
3
5
7
10
16
20
34
42
55
66
75
Byte Array (String representation) = 00-00-01-03-05-07-0A-10-14-22-2A-37-42-4B
Value = 0
Result = 256
Value = 3
Result = 1283
Value = 7
Result = 2567
Value = 16
Result = 5136
Value = 34
Result = 10786
Value = 55
Result = 16951

สรุปและอธิบายผลลัพธ์

  • ToUInt16 อ่าน 2 ไบต์: low byte ก่อน (little-endian) บน x86/x64 — คือค่า = value[startIndex] + (value[startIndex+1] << 8)
  • ตัวอย่าง: index 1 = 20 (0x14), index 2 = 30 (0x1E) → 0x1E14 = 7700 decimal
  • BitConverter.IsLittleEndian บอก endianness ของระบบ (ปกติ true บน Windows x86/x64)
  • BitConverter.ToString(byte[]) แปลงทั้งอาเรย์เป็น hex string คั่นด้วย hyphen
  • เมธอดคู่: GetBytes(ushort) แปลง ushort → byte[2]

ตารางเมธอด BitConverter สำหรับแปลงจาก Byte Array

เมธอดคืนค่าจำนวนไบต์
ToBooleanbool1
ToCharchar2
ToInt16 / ToUInt16short / ushort2
ToInt32 / ToUInt32int / uint4
ToInt64 / ToUInt64long / ulong8
ToSinglefloat4
ToDoubledouble8

หมายเหตุ: Endianness และ Cross-platform

// หากต้องการ big-endian (network byte order) ให้สลับเอง หรือใช้ BinaryPrimitives (NET Core 2.1+)
ushort be = BinaryPrimitives.ReadUInt16BigEndian(bytes.AsSpan(index));
// หรือ manual:
ushort beManual = (ushort)((bytes[index] << 8) | bytes[index + 1]);