英語で読む

次の方法で共有


Encoding.ASCII プロパティ

定義

ASCII (7 ビット) 文字セットのエンコーディングを取得します。

public static System.Text.Encoding ASCII { get; }

プロパティ値

ASCII (7 ビット) 文字セットのエンコード。

次の例は、ascii の範囲外にある文字に対する ASCII エンコーディングの効果を示しています。

using System;
using System.Text;

class EncodingExample 
{
  public static void Main() 
  {
      // Create an ASCII encoding.
      Encoding ascii = Encoding.ASCII;
  
      // A Unicode string with two characters outside the ASCII code range.
      String unicodeString =
          "This unicode string contains two characters " +
          "with codes outside the ASCII code range, " +
          "Pi (\u03a0) and Sigma (\u03a3).";
      Console.WriteLine("Original string:");
      Console.WriteLine(unicodeString);

      // Save the positions of the special characters for later reference.
      int indexOfPi = unicodeString.IndexOf('\u03a0');
      int indexOfSigma = unicodeString.IndexOf('\u03a3');

      // Encode the string.
      Byte[] encodedBytes = ascii.GetBytes(unicodeString);
      Console.WriteLine();
      Console.WriteLine("Encoded bytes:");
      foreach (Byte b in encodedBytes) 
      {
          Console.Write("[{0}]", b);
      }
      Console.WriteLine();
  
      // Notice that the special characters have been replaced with
      // the value 63, which is the ASCII character code for '?'.
      Console.WriteLine();
      Console.WriteLine(
          "Value at position of Pi character: {0}",
          encodedBytes[indexOfPi]
          );
      Console.WriteLine(
          "Value at position of Sigma character: {0}",
          encodedBytes[indexOfSigma]
          );

      // Decode bytes back to a string.
      // Notice missing the Pi and Sigma characters.
      String decodedString = ascii.GetString(encodedBytes);
      Console.WriteLine();
      Console.WriteLine("Decoded bytes:");
      Console.WriteLine(decodedString);
  }
}
/*
This code produces the following output.

Original string:
This unicode string contains two characters with codes outside the ASCII code range, Pi (Π) and Sigma (Σ).

Encoded bytes:
[84][104][105][115][32][117][110][105][99][111][100][101][32][115][116][114][105][110][103][32][99][111][110][116][97][105][110][115][32][116][119][111][32][99][104][97][114][97][99][116][101][114][115][32][119][105][116][104][32][99][111][100][101][115][32][111][117][116][115][105][100][101][32][116][104][101][32][65][83][67][73][73][32][99][111][100][101][32][114][97][110][103][101][44][32][80][105][32][40][63][41][32][97][110][100][32][83][105][103][109][97][32][40][63][41][46]

Value at position of Pi character: 63
Value at position of Sigma character: 63

Decoded bytes:
This unicode string contains two characters with codes outside the ASCII code range, Pi (?) and Sigma (?).

*/

注釈

ASCII 文字は、U + 0000 から U + 007F までの 128 Unicode 文字に制限されています。

アプリの ASCII エンコードを選択するときは、次の点を考慮してください。

  • Ascii エンコーディングは、通常、ASCII を必要とするプロトコルに適しています。

  • 8 ビット エンコード (誤って "ASCII" と呼ばれることもあります) が必要な場合は、ASCII エンコードよりも UTF-8 エンコードをお勧めします。 文字が 0-7F の場合、結果は同じですが、UTF-8 を使用すると、表現可能なすべての Unicode 文字を表現できるため、データ損失を回避できます。 ASCII エンコーディングには、悪意のある使用を許可する8ビットのあいまいさがあることに注意してください。ただし、UTF-8 エンコーディングでは、8番目のビットのあいまいさが解消されます。

  • .NET Framework バージョン2.0 より前では、8番目のビットを無視することで、.NET Framework スプーフィングを許可しています。 .NET Framework 2.0 以降では、デコード中に非 ASCII コードポイントがフォールバックします。

ASCIIEncodingこのプロパティによって返されるオブジェクトに、アプリに対する適切な動作がない可能性があります。 この例では、置換フォールバックを使用して、エンコードできない各文字列と、デコードできない各バイトを疑問符 ("?") 文字で置き換えます。 代わりに、 GetEncoding(String, EncoderFallback, DecoderFallback) 次の例に示すように、メソッドを呼び出し ASCIIEncoding て、フォールバックがまたはのいずれかであるオブジェクトをインスタンス化でき EncoderFallbackExceptionDecoderFallbackException ます。

using System;
using System.Text;

public class Example
{
   public static void Main()
   {
      Encoding enc = Encoding.GetEncoding("us-ascii", 
                                          new EncoderExceptionFallback(),
                                          new DecoderExceptionFallback());
      string value = "\u00C4 \u00F6 \u00AE"; 
      
      try {
         byte[] bytes= enc.GetBytes(value);
         foreach (var byt in bytes)
            Console.Write("{0:X2} ", byt);
         Console.WriteLine();

         string value2 = enc.GetString(bytes);
         Console.WriteLine(value2);
      }
      catch (EncoderFallbackException e) {
         Console.WriteLine("Unable to encode {0} at index {1}", 
                           e.IsUnknownSurrogate() ? 
                              String.Format("U+{0:X4} U+{1:X4}", 
                                            Convert.ToUInt16(e.CharUnknownHigh),
                                            Convert.ToUInt16(e.CharUnknownLow)) :
                              String.Format("U+{0:X4}", 
                                            Convert.ToUInt16(e.CharUnknown)),
                           e.Index);
      }
   }
}
// The example displays the following output:
//        Unable to encode U+00C4 at index 0

適用対象

製品 バージョン
.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, 8, 9, 10
.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, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

こちらもご覧ください