Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hello, i need a help here. I have a string which corresponds to an utf8 encoded character in its hexadecimal form. for example:
C#
String s="c2b5";

Which is the character
µ
in the unicode table in its hex form. Can anyone tell me how can i print this char in a console or a file?
Thank you.


UPDATE:
This doesn't work, compiler error.
C#
String mystr = "\u"+"03a3";
Byte[] myb = utf8.GetBytes(mystr);
String dec = utf8.GetString(myb);
Console.WriteLine(dec);


Although this works
C#
String mystr = "\u03a3";
Byte[] myb = utf8.GetBytes(mystr);
String dec = utf8.GetString(myb);
Console.WriteLine(dec);
Posted
Updated 24-Oct-11 8:33am
v2
Comments
Sergey Alexandrovich Kryukov 24-Oct-11 14:43pm    
You're getting bytes of string with hex number. Why? You asked about character itself. Convert it to character first.
--SA

1 solution

No! The Greek µ is "03BC"! This is:

C#
char ch = '\x03bc';
//or
ch = '\u03bc';

//or
const string MuString = "03BC";
ushort utf16 = ushort.Parse(MuString, System.Globalization.NumberStyles.HexNumber);
ch = (char)utf16;


Now, you can write it into the file, but make sure the file is created with the encoding of one of Unicode UTF formats. For example:
C#
using (System.IO.StreamWriter writer = new System.IO.StreamWriter(fileName, true, System.Text.UTF8Encoding.UTF8)) {
    //...
    writer.Write(ch);
    //...
}


See http://msdn.microsoft.com/en-us/library/system.io.streamwriter.aspx[^].

—SA
 
Share this answer
 
v4
Comments
Member 3495631 24-Oct-11 14:57pm    
Thank you very much sir! It was exactly what I was searching.
Sergey Alexandrovich Kryukov 24-Oct-11 15:04pm    
Sure.
Good luck, call again.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900