Convert.FromBase64String(String) Convert.FromBase64String(String) Convert.FromBase64String(String) Convert.FromBase64String(String) Method

定義

將指定的字串 (將二進位資料編碼為 Base-64 位數) 轉換為相等的 8 位元不帶正負號的整數陣列。Converts the specified string, which encodes binary data as base-64 digits, to an equivalent 8-bit unsigned integer array.

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 String String String

要轉換的字串。The string to convert.

傳回

Byte[]

8 位元不帶正負號的整數且與 s 相等的陣列。An array of 8-bit unsigned integers that is equivalent to s.

例外狀況

忽略空白字元的 s 長度,不是零或 4 的倍數。The length of s, ignoring white-space characters, is not zero or a multiple of 4.

-或--or- s 的格式無效。The format of s is invalid. s 包含非 Base-64 字元、超過兩個填補字元,或在填補字元中有非空白字元。 s contains a non-base-64 character, more than two padding characters, or a non-white space-character among the padding characters.

範例

下列範例會使用ToBase64String(Byte[])方法,以將位元組陣列轉換成使用 uuencode 編碼 (base64) 字串,然後再呼叫FromBase64String(String)方法來還原原始的位元組陣列。The following example uses the ToBase64String(Byte[]) method to convert a byte array to a UUencoded (base-64) string, and then calls the FromBase64String(String) method to restore the original byte array.

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 sring.
       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
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 sring.
       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 個元素陣列。The following is a more complex example that creates a 20-element array of 32-bit integers. 然後它會使用BitConverter.GetBytes(Int32)方法,將每個項目轉換為位元組陣列,它會儲存在緩衝區中的適當位置呼叫Array.Copy(Array, Int32, Array, Int32, Int32)方法。It then uses the BitConverter.GetBytes(Int32) method to convert each element into a byte array, which it stores in the appropriate position in a buffer by calling the Array.Copy(Array, Int32, Array, Int32, Int32) method. 這個緩衝區會傳遞至ToBase64String(Byte[])方法來建立使用 uuencode 編碼 (base64) 字串。This buffer is then passed to the ToBase64String(Byte[]) method to create a UUencoded (base-64) string. 然後它會呼叫FromBase64String(String)方法來解碼的 uuencode 編碼字串和呼叫BitConverter.ToInt32方法,將每一組的四個位元組 (32 位元整數的大小) 轉換成整數。It then calls the FromBase64String(String) method to decode the UUencoded string, and calls the BitConverter.ToInt32 method to convert each set of four bytes (the size of a 32-bit integer) to an integer. 範例輸出顯示已成功還原原始的陣列。The output from the example shows that the original array has been successfully restored.

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 }
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 數字、 空格字元和尾端填補字元所組成。s is composed of base-64 digits, white-space characters, and trailing padding characters. Base-64 位數,按遞增順序由零是大寫字元"A"到"Z"、 小寫字元"a"到"z"、 數字"0"到"9",以及符號"+"和"/"。The base-64 digits in ascending order from zero are the uppercase characters "A" to "Z", lowercase characters "a" to "z", numerals "0" to "9", and the symbols "+" and "/".

空白字元,及其 Unicode 名稱和十六進位的字碼指標,都使用定位字元 (字元表格,U + 0009),新行換行字元 (U + 000A)、 歸位字元 (歸位字元、 U + 000d) 和空白 (空格,U + 0020)。The white-space characters, and their Unicode names and hexadecimal code points, are tab (CHARACTER TABULATION, U+0009), newline (LINE FEED, U+000A), carriage return (CARRIAGE RETURN, U+000D), and blank (SPACE, U+0020). 任意數目的泛空白字元可以出現在s因為所有的泛空白字元會被忽略。An arbitrary number of white-space characters can appear in s because all white-space characters are ignored.

過去的字元,"="、 用於尾端填補。The valueless character, "=", is used for trailing padding. 結尾s可以包含零個、 一個或兩個填補字元。The end of s can consist of zero, one, or two padding characters.

重要

FromBase64String方法專門設計用來處理單一字串,包含要解碼的所有資料。The FromBase64String method is designed to process a single string that contains all the data to be decoded. 若要從資料流的 base-64 字元資料解碼,使用System.Security.Cryptography.FromBase64Transform類別。To decode base-64 character data from a stream, use the System.Security.Cryptography.FromBase64Transform class.

適用於

另請參閱