Convert.FromBase64String(String) メソッド

定義

指定した文字列を変換します。これにより、バイナリ データは 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 ビット符号なし整数の配列。

例外

snullです。

空白文字を除いた s の長さが、0 でなく 4 の倍数でもありません。

または

s の形式が正しくありません。 s に Base-64 以外の文字が含まれるか、3 個以上の埋め込み文字があるか、または埋め込み文字の間に空白以外の文字が含まれています。

次の例では、このメソッドを 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 呼び出して、4 バイトの各セット (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 桁、空白文字、および末尾のパディング文字で構成されます。 0 から昇順の底 64 桁の数字は、大文字の "A" から "Z"、小文字の "a" から "z"、数字 "0" から "9"、および記号 "+" と "/" です。

空白文字とその Unicode 名と 16 進コード ポイントは、タブ (CHARACTER TABULATION、U+0009)、改行 (LINE FEED、U+000A)、キャリッジ リターン (キャリッジ リターン、U+000D)、空白 (SPACE、U+0020) です。 空白文字はすべて無視されるため、任意の数の s 空白文字が表示される可能性があります。

末尾の埋め込みには、値なしの文字 "=" が使用されます。 末尾 s は、0 文字、1 文字、または 2 文字のパディング文字で構成できます。

重要

この FromBase64String メソッドは、デコードするすべてのデータを含む 1 つの文字列を処理するように設計されています。 ストリームから base-64 文字データをデコードするには、クラスを使用します System.Security.Cryptography.FromBase64Transform

適用対象

こちらもご覧ください