เมธอด Convert.ToBase64String() ใน C# แปลงอาร์เรย์ของจำนวนเต็ม 8 บิตแบบไม่มีเครื่องหมาย (Byte Array) ให้เป็นสตริงที่เข้ารหัส Base-64
ไวยากรณ์
public static string ToBase64String(byte[] arr);
ตัวอย่าง: แปลงเป็น Base64 และกลับมาอีกครั้ง
using System;
public class Demo
{
public static void Main()
{
byte[] val1 = { 5, 10, 15, 20, 25, 30 };
string str = Convert.ToBase64String(val1);
Console.WriteLine("Base 64 string: '{0}'", str);
byte[] val2 = Convert.FromBase64String(str);
Console.WriteLine("Converted byte value: {0}", BitConverter.ToString(val2));
}
}
ผลลัพธ์
Base 64 string: 'BQoPFBke'
Converted byte value: 05-0A-0F-14-19-1E
การประยุกต์: ใช้เข้ารหัสข้อมูล Binary ให้เป็นข้อความ เพื่อส่งผ่านช่องทางที่รองรับเฉพาะ Text (เช่น JSON, XML, Email)