Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get ASCII value of string in C#

Tags:

c#

encoding

ascii

People also ask

How do I check if a string is ASCII?

isascii() will check if the strings is ascii. "\x03". isascii() is also True.

What is the ASCII value of character C in C programming?

ASCII contains numbers, each character has its own number to represent. We have 256 character to represent in C (0 to 255) like character (a-z, A-Z), digits (0-9) and special character like !, @, # etc. This each ASCII code occupied with 7 bits in the memory. Let suppose ASCII value of character 'C' is 67.


From MSDN

string value = "9quali52ty3";

// Convert the string into a byte[].
byte[] asciiBytes = Encoding.ASCII.GetBytes(value);

You now have an array of the ASCII value of the bytes. I got the following:

57 113 117 97 108 105 53 50 116 121 51


string s = "9quali52ty3";
foreach(char c in s)
{
  Console.WriteLine((int)c);
}

This should work:

string s = "9quali52ty3";
byte[] ASCIIValues = Encoding.ASCII.GetBytes(s);
foreach(byte b in ASCIIValues) {
    Console.WriteLine(b);
}

Do you mean you only want the alphabetic characters and not the digits? So you want "quality" as a result? You can use Char.IsLetter or Char.IsDigit to filter them out one by one.

string s = "9quali52ty3";
StringBuilder result = new StringBuilder();
foreach(char c in s)
{
  if (Char.IsLetter(c))  
    result.Add(c);
}
Console.WriteLine(result);  // quality

string text = "ABCD";
for (int i = 0; i < text.Length; i++)
{
  Console.WriteLine(text[i] + " => " + Char.ConvertToUtf32(text, i));
}

If I remember correctly, the ASCII value is the number of the lower seven bits of the Unicode number.


string value = "mahesh";

// Convert the string into a byte[].
byte[] asciiBytes = Encoding.ASCII.GetBytes(value);

for (int i = 0; i < value.Length; i++)


    {
        Console.WriteLine(value.Substring(i, 1) + " as ASCII value of: " + asciiBytes[i]);
    }

This program will accept more than one character and output their ASCII value:

using System;
class ASCII
{
    public static void Main(string [] args)
    {
        string s;
        Console.WriteLine(" Enter your sentence: ");
        s = Console.ReadLine();
        foreach (char c in s)
        {
            Console.WriteLine((int)c);
        }
    }
}