คลาส BitConverter ใน C# ใช้สำหรับแปลงข้อมูลชนิดพื้นฐาน (base data types) เป็นอาเรย์ไบต์ (byte array) และแปลงกลับจากอาเรย์ไบต์เป็นชนิดข้อมูลพื้นฐาน
เมธอดหลักของ BitConverter
| เมธอด | คำอธิบาย |
|---|---|
| DoubleToInt64Bits(Double) | แปลง double-precision floating-point เป็น signed 64-bit integer |
| GetBytes(Boolean) | แปลงค่า Boolean เป็น byte array |
| GetBytes(Char) | แปลง Unicode character เป็น byte array |
| GetBytes(Double) | แปลง double-precision floating-point เป็น byte array |
| GetBytes(Int16) | แปลง 16-bit signed integer เป็น byte array |
| GetBytes(Int32) | แปลง 32-bit signed integer เป็น byte array |
| Int64BitsToDouble(Int64) | แปลง 64-bit signed integer เป็น double-precision floating-point |
| ToBoolean(Byte[], Int32) | แปลงไบต์ที่ตำแหน่งที่ระบุเป็น Boolean |
| ToChar(Byte[], Int32) | แปลง 2 ไบต์ที่ตำแหน่งที่ระบุเป็น Unicode character |
| ToString(Byte[]) | แปลง byte array เป็นสตริง十六进制 (เช่น "0A-1F-3C") |
| ToString(Byte[], Int32) | แปลง subarray เป็นสตริง hex เริ่มจากตำแหน่งที่ระบุ |
| ToString(Byte[], Int32, Int32) | แปลง subarray เป็นสตริง hex เริ่มจากตำแหน่งที่ระบุ ความยาวที่ระบุ |
| ToUInt16(Byte[], Int32) | แปลง 2 ไบต์ที่ตำแหน่งที่ระบุเป็น 16-bit unsigned integer |
ตัวอย่างที่ 1: BitConverter.ToBoolean()
แปลงไบต์ที่ตำแหน่งที่ระบุเป็น Boolean — ค่า 0 คือ false, ค่าอื่นคือ true
using System;
public class Demo
{
public static void Main()
{
byte[] arr = { 50, 100 };
Console.WriteLine("Array values...");
for (int i = 0; i < arr.Length; i++)
{
Console.WriteLine("{0}", arr[i]);
}
Console.WriteLine("\nConverted values...");
for (int index = 0; index < arr.Length; index++)
{
bool res = BitConverter.ToBoolean(arr, index);
Console.WriteLine(res);
}
}
}
ผลลัพธ์
Array values... 50 100 Converted values... True True
ตัวอย่างที่ 2: BitConverter.DoubleToInt64Bits()
แปลง double เป็น 64-bit signed integer (raw bits representation)
using System;
public class Demo
{
public static void Main()
{
double d = 5.646587687;
Console.Write("Value = " + d);
long res = BitConverter.DoubleToInt64Bits(d);
Console.Write("\n64-bit signed integer = " + res);
}
}
ผลลัพธ์
Value = 5.646587687 64-bit signed integer = 4618043510978159912
ตัวอย่างเพิ่มเติม: GetBytes และ ToString
using System;
public class Demo
{
public static void Main()
{
int value = 123456;
byte[] bytes = BitConverter.GetBytes(value);
Console.WriteLine("Int32: " + value);
Console.WriteLine("Bytes (hex): " + BitConverter.ToString(bytes));
// Output: 40-E2-01-00 (little-endian บน x86/x64)
double d = 3.14159;
byte[] dBytes = BitConverter.GetBytes(d);
Console.WriteLine("Double: " + d);
Console.WriteLine("Bytes (hex): " + BitConverter.ToString(dBytes));
// แปลงกลับ
int back = BitConverter.ToInt32(bytes, 0);
double dBack = BitConverter.ToDouble(dBytes, 0);
Console.WriteLine("Back to int: " + back);
Console.WriteLine("Back to double: " + dBack);
}
}
ข้อสังเกตสำคัญ
- Endianness: BitConverter ใช้ endianness ของระบบ (ปกติคือ little-endian บน x86/x64) — ใช้
BitConverter.IsLittleEndianตรวจสอบได้ - หากต้องการควบคุม endianness เอง ให้ใช้
BinaryPrimitives(NET Core 2.1+/NET 5+) หรือเขียนเอง ToString(byte[])คืนค่า hex string คั่นด้วย hyphen (เช่น "40-E2-01-00")- เมธอด ToInt16, ToInt32, ToInt64, ToUInt16, ToUInt32, ToUInt64, ToSingle, ToDouble, ToChar, ToBoolean ใช้แปลงจาก byte array กลับ
- ใช้ใน: serialization, network protocols, file formats, cryptography, interop