How to show Unicode value of a string/char in C#

  • Thread starter Thread starter Nick
  • Start date Start date
N

Nick

Hi,

I would like to know what method can show the unicode value of string
and char in C#

Thanks.
 
Nick said:
I would like to know what method can show the unicode value of string
and char in C#

Well, a string is just a series of characters, so access each one in
turn. You can find the unicode value of a character by just casting it
char. For instance, this prints out the unicode value of each character
in a string:

foreach (char c in theString)
{
Console.WriteLine ((int)c);
}
 
Thanks
Well, a string is just a series of characters, so access each one in
turn. You can find the unicode value of a character by just casting it
char. For instance, this prints out the unicode value of each character
in a string:

foreach (char c in theString)
{
Console.WriteLine ((int)c);
}
 
Nick,

N> I would like to know what method can show the unicode value of string
N> and char in C#

using System;

class UnicodeValues {
static void Main() {¹
char c = (char) 0x0023; // '#'¹
long l = (long) c;
Console.WriteLine(c.ToString());
Console.WriteLine(l.ToString());
}
}

HTH,

Stefan
 
long is a System.Int64, you'd be better using a short, since it's a 16bit
type like char.

Etienne Boucher
 
Etienne Boucher said:
long is a System.Int64, you'd be better using a short, since it's a 16bit
type like char.

I personally tend to use int. Short would be okay, but ushort would be
better, as that's an unsigned 16-bit type like char.
 
Back
Top