Student
Student

Reputation: 3499

How to convert a string to ASCII

How do I convert each letter in a string to its ASCII character value?

Upvotes: 31

Views: 200222

Answers (8)

Marco Merola
Marco Merola

Reputation: 879

byte[] bytes = Encoding.ASCII.GetBytes(inputString);

In the ASCII enconding each char is represented with 1 byte, however in C# the string type uses 2 bytes per char. This happens because .NET uses UTF-16 to encode the text in a string. https://learn.microsoft.com/en-us/dotnet/standard/base-types/character-encoding-introduction

Hence, if you want to keep an ASCII "string" in memory, the most efficient way is to use a byte array. You can always convert it back to string, but remember it will double in size in memory because as soon as it is converted to String it gets converted to UTF-16.

string value = new ASCIIEncoding().GetString(bytes);

Upvotes: 6

dodgy_coder
dodgy_coder

Reputation: 13033

Here is an extension method based on Jon Skeet's answer:

    public static string ConvertUnicodeStringToAscii(this string text)
    {
        var sb = new StringBuilder();
        foreach (char c in text)
        {
            int unicode = c;
            if (unicode < 128)
            {
                sb.Append(c);
            }
        }
        return sb.ToString();
    }

Upvotes: 1

Ahmed Saber
Ahmed Saber

Reputation: 89

You can do it by using LINQ-expression.

  public static List<int> StringToAscii(string value)
  {
        if (string.IsNullOrEmpty(value))
            throw new ArgumentException("Value cannot be null or empty.", nameof(value));

        return value.Select(System.Convert.ToInt32).ToList();
  } 

Upvotes: 0

Student
Student

Reputation: 3499

For Any String try this:

string s = Console.ReadLine();
foreach( char c in s)
{
    Console.WriteLine(System.Convert.ToInt32(c));
}
Console.ReadKey();

Upvotes: 25

user2956314
user2956314

Reputation: 174

Try Linq:

Result = string.Join("", input.ToCharArray().Where(x=> ((int)x) < 127));

This will filter out all non ascii characters. Now if you want an equivalent, try the following:

Result = string.Join("", System.Text.Encoding.ASCII.GetChars(System.Text.Encoding.ASCII.GetBytes(input.ToCharArray())));

Upvotes: 4

Akshatha
Akshatha

Reputation: 597

Use Convert.ToInt32() for conversion. You can have a look at How to convert string to ASCII value in C# and ASCII values.

Upvotes: 0

r12
r12

Reputation: 59

I think this code may be help you:

string str = char.ConvertFromUtf32(65)

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1499750

.NET stores all strings as a sequence of UTF-16 code units. (This is close enough to "Unicode characters" for most purposes.)

Fortunately for you, Unicode was designed such that ASCII values map to the same number in Unicode, so after you've converted each character to an integer, you can just check whether it's in the ASCII range. Note that you can use an implicit conversion from char to int - there's no need to call a conversion method:

string text = "Here's some text including a \u00ff non-ASCII character";
foreach (char c in text)
{
    int unicode = c;
    Console.WriteLine(unicode < 128 ? "ASCII: {0}" : "Non-ASCII: {0}", unicode);
}

Upvotes: 32

Related Questions