Convert.FromBase64String(String) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将指定的字符串(它将二进制数据编码为 Base64 数字)转换为等效的 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[]
与 s
等效的 8 位无符号整数数组。
例外
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
由 base-64 数字、空格字符和尾随填充字符组成。 从零开始的基数 64 位数字是大写字符“A”到“Z”,小写字符“a”到“z”,数字“0”到“9”,符号“+”和“/”。
空格字符及其 Unicode 名称和十六进制代码位是制表符 (字符制表符、U+0009) 、换行符 (行源、U+000A) 、回车 (回车、U+000D) 和空白 (空格、U+0020) 。 任意数量的空白字符可以出现在其中 s
,因为所有空白字符将被忽略。
无值字符“=”用于尾随填充。 结尾 s
可以包含零个、一个或两个填充字符。
重要
该方法 FromBase64String 旨在处理包含要解码的所有数据的单个字符串。 若要从流解码 base-64 字符数据,请使用 System.Security.Cryptography.FromBase64Transform 该类。