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

Definizione

Converte la stringa specificata, che codifica dati binari come cifre base 64, in una matrice di interi senza segno a 8 bit equivalente.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()

Parametri

s
String String String String

Stringa da convertire.The string to convert.

Restituisce

Byte[]

Matrice di interi senza segno a 8 bit equivalente a s.An array of 8-bit unsigned integers that is equivalent to s.

Eccezioni

La lunghezza di s, esclusi i caratteri di spazio vuoto, non è zero o un multiplo di 4.The length of s, ignoring white-space characters, is not zero or a multiple of 4.

In alternativa-or- Il formato di s non è valido.The format of s is invalid. s contiene un carattere non Base 64, più di due caratteri di riempimento o uno spazio non vuoto tra i caratteri di riempimento.s contains a non-base-64 character, more than two padding characters, or a non-white space-character among the padding characters.

Esempi

L'esempio seguente usa il ToBase64String(Byte[]) metodo per convertire una matrice di byte in una stringa UUEncode (base-64) e quindi chiama il FromBase64String(String) metodo per ripristinare la matrice di byte originale.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

Di seguito è riportato un esempio più complesso che crea una matrice di 20 elementi di interi a 32 bit.The following is a more complex example that creates a 20-element array of 32-bit integers. Quindi, utilizza il BitConverter.GetBytes(Int32) metodo per convertire ogni elemento in una matrice di byte, che viene archiviato nella posizione appropriata in un buffer chiamando il Array.Copy(Array, Int32, Array, Int32, Int32) (metodo).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. Questo buffer viene quindi passato al ToBase64String(Byte[]) metodo per creare una stringa UUEncode (base-64).This buffer is then passed to the ToBase64String(Byte[]) method to create a UUencoded (base-64) string. Chiama poi il FromBase64String(String) metodo da decodificare la stringa UUEncode e chiama il BitConverter.ToInt32 metodo per convertire ogni set di quattro byte (le dimensioni di un intero a 32 bit) in un numero intero.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. L'output dell'esempio mostra che la matrice originale è stata ripristinata.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 }

Commenti

s è composto da cifre base 64, gli spazi vuoti e caratteri di riempimento finali.s is composed of base-64 digits, white-space characters, and trailing padding characters. Le cifre base 64 in ordine crescente da zero sono i caratteri maiuscoli "A" a "Z", caratteri minuscoli "a" a "z", caratteri numerici da "0" a "9" e i simboli "+" e "/".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 "/".

I caratteri spazi vuoti e i punti di codice esadecimale e i nomi Unicode sono scheda (carattere di tabulazione, u+0009), nuova riga (avanzamento riga, U + 000A), ritorno a capo (ritorno a capo, U+000D) e spazio vuoto (spazio, U+10000 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). Un numero arbitrario di spazi vuoti può essere visualizzati s perché tutti i caratteri spazi vuoti vengono ignorati.An arbitrary number of white-space characters can appear in s because all white-space characters are ignored.

Il carattere non significativo "=", viene usato per la spaziatura interna finale.The valueless character, "=", is used for trailing padding. Fine del s può essere costituito da caratteri di riempimento pari a zero, uno o due.The end of s can consist of zero, one, or two padding characters.

Importante

Il FromBase64String metodo è progettato per elaborare una singola stringa che contiene tutti i dati da decodificare.The FromBase64String method is designed to process a single string that contains all the data to be decoded. Per decodificare i dati di caratteri base 64 da un flusso, utilizzare il System.Security.Cryptography.FromBase64Transform classe.To decode base-64 character data from a stream, use the System.Security.Cryptography.FromBase64Transform class.

Si applica a

Vedi anche