เมธอด Convert.FromBase64String(String) ใน C# แปลงสตริงที่เข้ารหัส Base-64 กลับเป็นอาร์เรย์ Byte (8-bit Unsigned Integer Array)
ไวยากรณ์
public static byte[] FromBase64String(string str);
ตัวอย่าง: ถอดรหัส Base64 กลับเป็น Byte Array
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
คู่กับ: Convert.ToBase64String() — ใช้คู่กันสำหรับ Encode/Decode ข้อมูล Binary เป็น Text และกลับมา