You are here:

Home > Documentation > .NET > .NET Core > API Reference > System.Text > ASCIIEncoding

ASCIIEncoding Class

Represents an ASCII (American Standard Code for Information Interchange) character encoding.

Namespace: System.Text
Assembly: System.Runtime.Extensions (in System.Runtime.Extensions.dll)

Inheritance: Object > Encoding > ASCIIEncoding

Remarks

The ASCII encoding uses 7 bits for each character, representing 128 characters. It is a subset of the UTF-8 encoding and is commonly used for plain text files and in older network protocols.

When converting strings to byte arrays using ASCIIEncoding, characters that are not representable in the ASCII character set (Unicode code points from 0x80 to 0xFF) are replaced with a question mark character ('?').

Methods

Member Description
GetBytes
public override byte[] GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex)
Encodes all the characters in the specified array into a sequence of bytes.
GetCharCount
public override int GetCharCount(byte[] bytes, int index, int count)
Calculates the number of characters produced by decoding a sequence of bytes.
GetChars
public override char[] GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex)
Decodes all the bytes in the specified array into a sequence of characters.
GetEncoding
public static Encoding GetEncoding(int codepage)
Returns an Encoding object for the specified code page.
GetString
public string GetString(byte[] bytes)
Converts an array of bytes to an equivalent string.

Properties

Member Description
EncodingName
public override string EncodingName { get; }
Gets the Web name for the encoding.
IsBrowserEncodable
public override bool IsBrowserEncodable { get; }
Gets a value indicating whether the encoding can be used by browsers.

Example

Converting a string to ASCII bytes

using System;
using System.Text;

public class Example
{
    public static void Main()
    {
        string originalString = "Hello, ASCII!";
        ASCIIEncoding ascii = new ASCIIEncoding();
        byte[] asciiBytes = ascii.GetBytes(originalString);

        Console.WriteLine($"Original String: {originalString}");
        Console.Write("ASCII Bytes: ");
        foreach (byte b in asciiBytes)
        {
            Console.Write($"{b:X2} ");
        }
        Console.WriteLine();

        string decodedString = ascii.GetString(asciiBytes);
        Console.WriteLine($"Decoded String: {decodedString}");

        string euroSymbol = "€"; // Not representable in ASCII
        byte[] euroBytes = ascii.GetBytes(euroSymbol);
        Console.WriteLine($"\nOriginal String (Euro Symbol): {euroSymbol}");
        Console.Write("ASCII Bytes (Euro Symbol): ");
        foreach (byte b in euroBytes)
        {
            Console.Write($"{b:X2} ");
        }
        Console.WriteLine();
        Console.WriteLine($"Decoded String (Euro Symbol): {ascii.GetString(euroBytes)}"); // Will be '?'
    }
}

Converting a string to ASCII bytes

Imports System
Imports System.Text

Public Module Example
    Public Sub Main()
        Dim originalString As String = "Hello, ASCII!"
        Dim ascii As New ASCIIEncoding()
        Dim asciiBytes As Byte() = ascii.GetBytes(originalString)

        Console.WriteLine($"Original String: {originalString}")
        Console.Write("ASCII Bytes: ")
        For Each b As Byte In asciiBytes
            Console.Write($"{b:X2} ")
        Next
        Console.WriteLine()

        Dim decodedString As String = ascii.GetString(asciiBytes)
        Console.WriteLine($"Decoded String: {decodedString}")

        Dim euroSymbol As String = "€" ' Not representable in ASCII
        Dim euroBytes As Byte() = ascii.GetBytes(euroSymbol)
        Console.WriteLine()
        Console.WriteLine($"Original String (Euro Symbol): {euroSymbol}")
        Console.Write("ASCII Bytes (Euro Symbol): ")
        For Each b As Byte In euroBytes
            Console.Write($"{b:X2} ")
        Next
        Console.WriteLine()
        Console.WriteLine($"Decoded String (Euro Symbol): {ascii.GetString(euroBytes)}") ' Will be '?'
    End Sub
End Module

Requirements

Assembly Version
System.Runtime.Extensions 4.0.0.0
.NET Framework 4.6 4.6.0.0
.NET Core 1.0 1.0.0.0