英語で読む

次の方法で共有


Convert.FromBase64String(String) メソッド

定義

指定した文字列を変換します。これにより、バイナリ データは Base64 の数字として等価の 8 ビット符号なし整数配列にエンコードされます。

C#
public static byte[] FromBase64String (string s);

パラメーター

s
String

変換する文字列。

戻り値

Byte[]

s と等価な 8 ビット符号なし整数の配列。

例外

snullです。

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

または

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

次の例では、このメソッドを ToBase64String(Byte[]) 使用してバイト配列を UUencoded (base-64) 文字列に変換し、メソッドを FromBase64String(String) 呼び出して元のバイト配列を復元します。

C#
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

32 ビット整数の 20 要素配列を作成するより複雑な例を次に示します。 その後、メソッドを BitConverter.GetBytes(Int32) 使用して各要素をバイト配列に変換します。この配列は、メソッドを呼び出してバッファー内の適切な位置に Array.Copy(Array, Int32, Array, Int32, Int32) 格納します。 その後、このバッファーがメソッドに ToBase64String(Byte[]) 渡され、UUencoded (base-64) 文字列が作成されます。 次に FromBase64String(String) 、UUencoded 文字列をデコードするメソッドを呼び出し、メソッドを BitConverter.ToInt32 呼び出して、4 バイトの各セット (32 ビット整数のサイズ) を整数に変換します。 この例の出力は、元の配列が正常に復元されたことを示しています。

C#
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 }

注釈

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

適用対象

製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

こちらもご覧ください