此方法用於將指定的字符串轉換為等效的8位無符號整數數組,該字符串將二進製數據編碼為64位基數。
用法:
public static byte[] FromBase64String (string s);
此處,s是要轉換的字符串。
返回值:此方法返回一個等效於s的8位無符號整數數組。
異常
- ArgumentNullException:如果s為null。
- FormatException:如果s的長度(不考慮空格字符)不是零或4的倍數,或者s的格式無效。 s包含非base-64字符,兩個以上的填充字符或填充字符中的非空白字符。
以下示例程序旨在說明Convert.FromBase64String(String)方法的用法:
示例1:
// C# program to demonstrate the
// Convert.FromBase64String(String) Method
using System;
class GFG {
// Main Method
public static void Main()
{
try {
// defining and initializing
// byte1 and byte2
byte[] byte1 = {2, 4, 6, 8, 10,
12, 14, 16, 18, 20};
byte[] byte2 = {10, 20, 30,
40, 50};
// calling get() Method
get(byte1, "byte1");
Console.WriteLine("");
get(byte2, "byte2");
}
catch (ArgumentNullException e) {
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
catch (FormatException e) {
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(),
e.Message);
}
}
// Defining get() method
public static void get(byte[] bytes, string str)
{
Console.WriteLine("For {0}", str);
// converting byte to base 64 string
string s = Convert.ToBase64String(bytes);
Console.WriteLine("The base 64 string: '{0}'", s);
Console.WriteLine("The base 64 string: '{0}'", s);
// converting base 64 string to byte array
byte[] val = Convert.FromBase64String(s);
Console.WriteLine("Converted byte value: {0}",
BitConverter.ToString(val));
}
}
輸出:
For byte1 The base 64 string: 'AgQGCAoMDhASFA==' The base 64 string: 'AgQGCAoMDhASFA==' Converted byte value: 02-04-06-08-0A-0C-0E-10-12-14 For byte2 The base 64 string: 'ChQeKDI=' The base 64 string: 'ChQeKDI=' Converted byte value: 0A-14-1E-28-32
示例2:對於ArgumentNullException
// C# program to demonstrate the
// Convert.FromBase64String(String) Method
using System;
class GFG {
// Main Method
public static void Main()
{
try {
// defining and initializing
// byte1 and byte2
byte[] byte1 = {2, 4, 6, 8, 10, 12,
14, 16, 18, 20};
// calling get() Method
get(byte1, "byte1");
Console.WriteLine("");
// converting base 64 string to byte array
Console.WriteLine("s is null");
byte[] val = Convert.FromBase64String(null);
Console.WriteLine("Converted byte value: {0}",
BitConverter.ToString(val));
}
catch (ArgumentNullException e) {
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
catch (FormatException e) {
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
}
// Defining get() method
public static void get(byte[] bytes, string str)
{
Console.WriteLine("For {0}", str);
// converting byte to base 64 string
string s = Convert.ToBase64String(bytes);
Console.WriteLine("The base 64 string: '{0}'", s);
// converting base 64 string to byte array
byte[] val = Convert.FromBase64String(s);
Console.WriteLine("Converted byte value: {0}",
BitConverter.ToString(val));
}
}
輸出:
For byte1 The base 64 string: 'AgQGCAoMDhASFA==' Converted byte value: 02-04-06-08-0A-0C-0E-10-12-14 s is null Exception Thrown: System.ArgumentNullException
示例3:對於FormatException
// C# program to demonstrate the
// Convert.FromBase64String(String) Method
using System;
class GFG {
// Main Method
public static void Main()
{
try {
// defining and initializing
// byte1 and byte2
byte[] byte1 = {2, 4, 6, 8, 10,
12, 14, 16, 18, 20};
// calling get() Method
get(byte1, "byte1");
Console.WriteLine("");
// converting base 64
// string to byte array
Console.WriteLine("s contains a non-base-64 character");
byte[] val = Convert.FromBase64String("sun");
Console.WriteLine("Converted byte value: {0}",
BitConverter.ToString(val));
}
catch (ArgumentNullException e) {
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
catch (FormatException e) {
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
}
// Defining get() method
public static void get(byte[] bytes, string str)
{
Console.WriteLine("For {0}", str);
// converting byte to base 64 string
string s = Convert.ToBase64String(bytes);
Console.WriteLine("The base 64 string: '{0}'", s);
// converting base 64 string to byte array
byte[] val = Convert.FromBase64String(s);
Console.WriteLine("Converted byte value: {0}",
BitConverter.ToString(val));
}
}
輸出:
For byte1 The base 64 string: 'AgQGCAoMDhASFA==' Converted byte value: 02-04-06-08-0A-0C-0E-10-12-14 s contains a non-base-64 character Exception Thrown: System.FormatException
參考:
相關用法
- C# DateTimeOffset.Add()用法及代碼示例
- C# String.Contains()用法及代碼示例
- C# Math.Sin()用法及代碼示例
- C# Math.Cos()用法及代碼示例
- C# Dictionary.Add()用法及代碼示例
- C# Math.Tan()用法及代碼示例
- C# Math.Abs()方法用法及代碼示例
- C# Math.Exp()用法及代碼示例
- C# Math.Abs()函數用法及代碼示例
注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 C# | Convert.FromBase64String(String) Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。