C# .NET - How to get unicode in hexa decimal of a character

Asked By balaji mogadali on 22-Sep-11 03:11 AM
hi frds
how to get unicode in hexa decimal of a character using c#
Devil Scorpio replied to balaji mogadali on 22-Sep-11 03:17 AM
Hi Balaji,

You need to convert the Hex back into an int then convert the into to a char.

string sHex = "41"; // same as '<strong class="highlight">A</strong>'
byte newByte = byte.Parse(sHex, System.Globalization.NumberStyles.HexNumber);
// newByte now = "65"
int i = Convert.ToInt32(newByte.ToString()); // i now = 65
Char schar = Convert.ToChar(i);
// schar = '<strong class="highlight">A</strong>'
aneesa replied to balaji mogadali on 22-Sep-11 03:17 AM
   string text = "HELLOWORLD";
  foreach (char in text)
  {
   Console.WriteLine("{0} U+{1:x4} {2}", c, (int)c, (int)c);
  }
aneesa replied to balaji mogadali on 22-Sep-11 03:18 AM

You want to use the http://msdn.microsoft.com/en-us/library/system.char.convertfromutf32.aspx function.

string codePoint = "...";

int code = int.Parse(codePoint, System.Globalization.NumberStyles.HexNumber);
string unicodeString = char.ConvertFromUtf32(code).ToString();
dipa ahuja replied to balaji mogadali on 22-Sep-11 03:31 AM
Untitled document
private string HexString2Ascii(string hexString)
{
   StringBuilder sb = new StringBuilder();
 
  for (int i = 0; i <= hexString.Length - 2; i += 2)
   {
     sb.Append(Convert.ToString(Convert.ToChar(Int32.Parse(hexString.Substring(i, 2), System.Globalization.NumberStyles.HexNumber))));

   }
   return sb.ToString();
}