How to get ASCII value of string in C#

C#EncodingAscii

C# Problem Overview


I want to get the ASCII value of characters in a string in C#.

If my string has the value "9quali52ty3", I want an array with the ASCII values of each of the 11 characters.

How can I get ASCII values in C#?

C# Solutions


Solution 1 - C#

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

Solution 2 - C#

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

Solution 3 - C#

This should work:

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

Solution 4 - C#

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

Solution 5 - C#

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.

Solution 6 - C#

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]);
    }

Solution 7 - C#

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);
        }
    }
}

Solution 8 - C#

If you want the charcode for each character in the string, you could do something like this:

char[] chars = "9quali52ty3".ToCharArray();

Solution 9 - C#

byte[] asciiBytes = Encoding.ASCII.GetBytes("Y");
foreach (byte b in asciiBytes)
{
    MessageBox.Show("" + b);
}

Solution 10 - C#

Or in LINQ:

string value = "9quali52ty3";
var ascii_values = value.Select(x => (int)x);
var as_hex = value.Select(x => ((int)x).ToString("X02"));

Solution 11 - C#

Earlier responders have answered the question but have not provided the information the title led me to expect. I had a method that returned a one character string but I wanted a character which I could convert to hexadecimal. The following code demonstrates what I thought I would find in the hope it is helpful to others.

  string s = "\ta£\x0394\x221A";   // tab; lower case a; pound sign; Greek delta;
                                   // square root  
  Debug.Print(s);
  char c = s[0];
  int i = (int)c;
  string x = i.ToString("X");
  c = s[1];
  i = (int)c;
  x = i.ToString("X");
  Debug.Print(c.ToString() + " " + i.ToString() + " " + x);
  c = s[2];
  i = (int)c;
  x = i.ToString("X");
  Debug.Print(c.ToString() + " " + i.ToString() + " " + x);
  c = s[3];
  i = (int)c;
  x = i.ToString("X");
  Debug.Print(c.ToString() + " " + i.ToString() + " " + x);
  c = s[4];
  i = (int)c;
  x = i.ToString("X");
  Debug.Print(c.ToString() + " " + i.ToString() + " " + x);

The above code outputs the following to the immediate window:

a£Δ√

a 97 61

£ 163 A3

Δ 916 394

√ 8730 221A

Solution 12 - C#

You can remove the BOM using:

//Create a character to compare BOM
char byteOrderMark = (char)65279;
if (sourceString.ToCharArray()[0].Equals(byteOrderMark))
{
    targetString = sourceString.Remove(0, 1);
}

Solution 13 - C#

I want to get the ASCII value of characters in a string in C#.

Everyone confer answer in this structure. If my string has the value "9quali52ty3", I want an array with the ASCII values of each of the 11 characters.

but in console we work frankness so we get a char and print the ASCII code if i wrong so please correct my answer.

 static void Main(string[] args)
        {
            Console.WriteLine(Console.Read());
            Convert.ToInt16(Console.Read());
            Console.ReadKey();
        }

Solution 14 - C#

Why not the old fashioned easy way?

    public int[] ToASCII(string s)
    {
        char c;
        int[] cByte = new int[s.Length];   / the ASCII string
        for (int i = 0; i < s.Length; i++)
        {
            c = s[i];                        // get a character from the string s
            cByte[i] = Convert.ToInt16(c);   // and convert it to ASCII
        }
        return cByte;
    }

Solution 15 - C#

    string nomFile = "9quali52ty3";  
    
     byte[] nomBytes = Encoding.ASCII.GetBytes(nomFile);
     string name = "";
     foreach (byte he in nomBytes)
     {
         name += he.ToString("X02");
     }
`
     Console.WriteLine(name);

// it's` better now ;)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRBSView Question on Stackoverflow
Solution 1 - C#Brig LamoreauxView Answer on Stackoverflow
Solution 2 - C#LeppyR64View Answer on Stackoverflow
Solution 3 - C#jasonView Answer on Stackoverflow
Solution 4 - C#Lars TruijensView Answer on Stackoverflow
Solution 5 - C#RauhotzView Answer on Stackoverflow
Solution 6 - C#maheshView Answer on Stackoverflow
Solution 7 - C#M hasanView Answer on Stackoverflow
Solution 8 - C#Neil BarnwellView Answer on Stackoverflow
Solution 9 - C#moleatomView Answer on Stackoverflow
Solution 10 - C#ZagNutView Answer on Stackoverflow
Solution 11 - C#Tony DallimoreView Answer on Stackoverflow
Solution 12 - C#Md Shahzad AdilView Answer on Stackoverflow
Solution 13 - C#MA NadeemView Answer on Stackoverflow
Solution 14 - C#F. HarthoornView Answer on Stackoverflow
Solution 15 - C#Monarc DevView Answer on Stackoverflow