Convert.FromBase64String(String) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
將指定的字串 (將二進位資料編碼為 Base-64 位數) 轉換為相等的 8 位元不帶正負號的整數陣列。
public:
static cli::array <System::Byte> ^ FromBase64String(System::String ^ s);
public static byte[] FromBase64String (string s);
static member FromBase64String : string -> byte[]
Public Shared Function FromBase64String (s As String) As Byte()
參數
- s
- String
要轉換的字串。
傳回
- Byte[]
8 位元不帶正負號的整數且與 s
相等的陣列。
例外狀況
s
為 null
。
範例
下列範例會 ToBase64String(Byte[]) 使用 方法將位元組陣列轉換成 UUencoded (base-64) 字串,然後呼叫 FromBase64String(String) 方法來還原原始位元組陣列。
using System;
public class Example
{
public static void Main()
{
// Define a byte array.
byte[] bytes = { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 };
Console.WriteLine("The byte array: ");
Console.WriteLine(" {0}\n", BitConverter.ToString(bytes));
// Convert the array to a base 64 string.
string s = Convert.ToBase64String(bytes);
Console.WriteLine("The base 64 string:\n {0}\n", s);
// Restore the byte array.
byte[] newBytes = Convert.FromBase64String(s);
Console.WriteLine("The restored byte array: ");
Console.WriteLine(" {0}\n", BitConverter.ToString(newBytes));
}
}
// The example displays the following output:
// The byte array:
// 02-04-06-08-0A-0C-0E-10-12-14
//
// The base 64 string:
// AgQGCAoMDhASFA==
//
// The restored byte array:
// 02-04-06-08-0A-0C-0E-10-12-14
open System
// Define a byte array.
let bytes = [| 2uy; 4uy; 6uy; 8uy; 10uy; 12uy; 14uy; 16uy; 18uy; 20uy |]
printfn $"The byte array:\n {BitConverter.ToString bytes}\n"
// Convert the array to a base 64 string.
let s = Convert.ToBase64String bytes
printfn $"The base 64 string:\n {s}\n"
// Restore the byte array.
let newBytes = Convert.FromBase64String s
printfn $"The restored byte array:\n {BitConverter.ToString newBytes}\n"
// The example displays the following output:
// The byte array:
// 02-04-06-08-0A-0C-0E-10-12-14
//
// The base 64 string:
// AgQGCAoMDhASFA==
//
// The restored byte array:
// 02-04-06-08-0A-0C-0E-10-12-14
Module Example
Public Sub Main()
' Define a byte array.
Dim bytes As Byte() = { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 }
Console.WriteLine("The byte array: ")
Console.WriteLine(" {0}", BitConverter.ToString(bytes))
Console.WriteLine()
' Convert the array to a base 64 string.
Dim s As String = Convert.ToBase64String(bytes)
Console.WriteLine("The base 64 string:{1} {0}{1}",
s, vbCrLf)
' Restore the byte array.
Dim newBytes As Byte() = Convert.FromBase64String(s)
Console.WriteLine("The restored byte array: ")
Console.WriteLine(" {0}", BitConverter.ToString(newBytes))
Console.WriteLine()
End Sub
End Module
' The example displays the following output:
' The byte array:
' 02-04-06-08-0A-0C-0E-10-12-14
'
' The base 64 string:
' AgQGCAoMDhASFA==
'
' The restored byte array:
' 02-04-06-08-0A-0C-0E-10-12-14
以下是更複雜的範例,其會建立 32 位整數的 20 個元素陣列。 然後它會使用 BitConverter.GetBytes(Int32) 方法,將每個元素轉換成位元組陣列,它會藉由呼叫 Array.Copy(Array, Int32, Array, Int32, Int32) 方法,將它儲存在緩衝區中的適當位置。 接著,這個緩衝區會傳遞至 ToBase64String(Byte[]) 方法,以建立 UUencoded (base-64) 字串。 然後它會 FromBase64String(String) 呼叫 方法來解碼 UUencoded 字串,並呼叫 BitConverter.ToInt32 方法,將每組四個位元組 (32 位整數的大小) 轉換為整數。 範例的輸出顯示原始陣列已成功還原。
using System;
public class Example
{
public static void Main()
{
// Define an array of 20 elements and display it.
int[] arr = new int[20];
int value = 1;
for (int ctr = 0; ctr <= arr.GetUpperBound(0); ctr++) {
arr[ctr] = value;
value = value * 2 + 1;
}
DisplayArray(arr);
// Convert the array of integers to a byte array.
byte[] bytes = new byte[arr.Length * 4];
for (int ctr = 0; ctr < arr.Length; ctr++) {
Array.Copy(BitConverter.GetBytes(arr[ctr]), 0,
bytes, ctr * 4, 4);
}
// Encode the byte array using Base64 encoding
String base64 = Convert.ToBase64String(bytes);
Console.WriteLine("The encoded string: ");
for (int ctr = 0; ctr <= base64.Length / 50; ctr++)
Console.WriteLine(base64.Substring(ctr * 50,
ctr * 50 + 50 <= base64.Length
? 50 : base64.Length - ctr * 50));
Console.WriteLine();
// Convert the string back to a byte array.
byte[] newBytes = Convert.FromBase64String(base64);
// Convert the byte array back to an integer array.
int[] newArr = new int[newBytes.Length/4];
for (int ctr = 0; ctr < newBytes.Length / 4; ctr ++)
newArr[ctr] = BitConverter.ToInt32(newBytes, ctr * 4);
DisplayArray(newArr);
}
private static void DisplayArray(Array arr)
{
Console.WriteLine("The array:");
Console.Write("{ ");
for (int ctr = 0; ctr < arr.GetUpperBound(0); ctr++) {
Console.Write("{0}, ", arr.GetValue(ctr));
if ((ctr + 1) % 10 == 0)
Console.Write("\n ");
}
Console.WriteLine("{0} {1}", arr.GetValue(arr.GetUpperBound(0)), "}");
Console.WriteLine();
}
}
// The example displays the following output:
// The array:
// { 1, 3, 7, 15, 31, 63, 127, 255, 511, 1023,
// 2047, 4095, 8191, 16383, 32767, 65535, 131071, 262143, 524287, 1048575 }
//
// The encoded string:
// AQAAAAMAAAAHAAAADwAAAB8AAAA/AAAAfwAAAP8AAAD/AQAA/w
// MAAP8HAAD/DwAA/x8AAP8/AAD/fwAA//8AAP//AQD//wMA//8H
//
// The array:
// { 1, 3, 7, 15, 31, 63, 127, 255, 511, 1023,
// 2047, 4095, 8191, 16383, 32767, 65535, 131071, 262143, 524287, 1048575 }
open System
let displayArray (arr: 'a[]) =
printfn "The array:"
printf "{ "
for i = 0 to arr.GetUpperBound(0) - 1 do
printf $"{arr[i]}, "
if (i + 1) % 10 = 0 then
printf "\n "
printfn $"{arr[arr.GetUpperBound 0]} }}\n"
// Define an array of 20 elements and display it.
let arr = Array.zeroCreate<int> 20
let mutable value = 1
for i = 0 to arr.GetUpperBound 0 do
arr[i] <- value
value <- value * 2 + 1
displayArray arr
// Convert the array of integers to a byte array.
let bytes = Array.zeroCreate<byte> (arr.Length * 4)
for i = 0 to arr.Length - 1 do
Array.Copy(BitConverter.GetBytes(arr[i]), 0, bytes, i * 4, 4)
// Encode the byte array using Base64 encoding
let base64 = Convert.ToBase64String bytes
printfn "The encoded string: "
printfn $"{base64}\n"
// Convert the string back to a byte array.
let newBytes = Convert.FromBase64String base64
// Convert the byte array back to an integer array.
let newArr = Array.zeroCreate<int> (newBytes.Length / 4)
for i = 0 to newBytes.Length / 4 - 1 do
newArr[i] <- BitConverter.ToInt32(newBytes, i * 4)
displayArray newArr
// The example displays the following output:
// The array:
// { 1, 3, 7, 15, 31, 63, 127, 255, 511, 1023,
// 2047, 4095, 8191, 16383, 32767, 65535, 131071, 262143, 524287, 1048575 }
//
// The encoded string:
// AQAAAAMAAAAHAAAADwAAAB8AAAA/AAAAfwAAAP8AAAD/AQAA/wMAAP8HAAD/DwAA/x8AAP8/AAD/fwAA//8AAP//AQD//wMA//8HAP//DwA=
//
// The array:
// { 1, 3, 7, 15, 31, 63, 127, 255, 511, 1023,
// 2047, 4095, 8191, 16383, 32767, 65535, 131071, 262143, 524287, 1048575 }
Module Example
Public Sub Main()
' Define an array of 20 elements and display it.
Dim arr(19) As Integer
Dim value As Integer = 1
For ctr As Integer = 0 To arr.GetUpperBound(0)
arr(ctr) = value
value = value * 2 + 1
Next
DisplayArray(arr)
' Convert the array of integers to a byte array.
Dim bytes(arr.Length * 4 - 1) As Byte
For ctr As Integer = 0 To arr.Length - 1
Array.Copy(BitConverter.GetBytes(arr(ctr)), 0,
bytes, ctr * 4, 4)
Next
' Encode the byte array using Base64 encoding
Dim base64 As String = Convert.ToBase64String(bytes)
Console.WriteLine("The encoded string: ")
For ctr As Integer = 0 To base64.Length \ 50 - 1
Console.WriteLine(base64.Substring(ctr * 50,
If(ctr * 50 + 50 <= base64.Length,
50, base64.Length - ctr * 50)))
Next
Console.WriteLine()
' Convert the string back to a byte array.
Dim newBytes() As Byte = Convert.FromBase64String(base64)
' Convert the byte array back to an integer array.
Dim newArr(newBytes.Length\4 - 1) As Integer
For ctr As Integer = 0 To newBytes.Length \ 4 - 1
newArr(ctr) = BitConverter.ToInt32(newBytes, ctr * 4)
Next
DisplayArray(newArr)
End Sub
Private Sub DisplayArray(arr As Array)
Console.WriteLine("The array:")
Console.Write("{ ")
For ctr As Integer = 0 To arr.GetUpperBound(0) - 1
Console.Write("{0}, ", arr.GetValue(ctr))
If (ctr + 1) Mod 10 = 0 Then Console.Write("{0} ", vbCrLf)
Next
Console.WriteLine("{0} {1}", arr.GetValue(arr.GetUpperBound(0)), "}")
Console.WriteLine()
End Sub
End Module
' The example displays the following output:
' The array:
' { 1, 3, 7, 15, 31, 63, 127, 255, 511, 1023,
' 2047, 4095, 8191, 16383, 32767, 65535, 131071, 262143, 524287, 1048575 }
'
' The encoded string:
' AQAAAAMAAAAHAAAADwAAAB8AAAA/AAAAfwAAAP8AAAD/AQAA/w
' MAAP8HAAD/DwAA/x8AAP8/AAD/fwAA//8AAP//AQD//wMA//8H
'
' The array:
' { 1, 3, 7, 15, 31, 63, 127, 255, 511, 1023,
' 2047, 4095, 8191, 16383, 32767, 65535, 131071, 262143, 524287, 1048575 }
備註
s
由 64 位數、空白字元和尾端填補字元所組成。 以零為基底 64 位數的遞增順序是大寫字元 「A」 到 「Z」、小寫字元 「a」 到 「z」、數位 「0」 到 「9」,以及符號 「+」 和 「/」。
空白字元及其 Unicode 名稱和十六進位字碼點為 tab (CHARACTER TABULATION、U+0009) 、分行符號 (LINE FEED、U+000A) 、歸位字元 (歸位字元、U+000D) ,以及空白 (SPACE、U+0020) 。 可能會顯示任意數目的空白字元, s
因為會忽略所有空白字元。
無值字元 「=」 用於尾端填補。 結尾 s
可以包含零、一或兩個填補字元。
重要
方法 FromBase64String 的設計目的是要處理包含要解碼之所有資料的單一字串。 若要從資料流程解碼 base-64 字元資料,請使用 System.Security.Cryptography.FromBase64Transform 類別。