How to Get Ascii Value of String in C#

How to get ASCII value of string in 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

Getting The ASCII Value of a character in a C# string

Here's an alternative since you don't like the cast to int:

foreach(byte b in System.Text.Encoding.UTF8.GetBytes(str.ToCharArray()))
Console.Write(b.ToString());

How to convert a string to ASCII

For Any String try this:

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

How to get ASCII value of a character

To my understanding the ASCII value can be obtained directly from the char representation as follows.

Select(x => new CharacterFrequency()
{
Char = x.Key,
Frequency = x.Count(),
Ascii = (byte)x.Key
}
)

How to get character for a given ascii value

Do you mean "A" (a string) or 'A' (a char)?

int unicode = 65;
char character = (char) unicode;
string text = character.ToString();

Note that I've referred to Unicode rather than ASCII as that's C#'s native character encoding; essentially each char is a UTF-16 code point.



Related Topics



Leave a reply



Submit