简体   繁体   中英

Unicode rendering in c#

I have an C# application where I am storing the code point value of a Unicode character to be displayed when the user correctly matches a normal specific string.

The thing is that when I am storing the code point value directly (say, \ﮀ) the application works fine. But when I am reading from a file or a variable that has the code point only (in this case FB80), I get a lot of wrongly rendered characters. Changing the stored value to \ﮀ or trying to add a "\\u\u0026quot; in front of the value both results in the system reading it as \ﮀ which ends up having another wrong result.

What is the way around this?

XmlTextReader reader = new XmlTextReader("file.xml"); 
reader.Read(); 
reader.MoveToAttribute("glyph"); 

glyph = reader.Value; 
// glyph will be "FB80" 
// if xml file had "\uFB80", glyph will be "\\uFB80" 

richTextBox1.SelectionFont = "QCF_P604"; 
richTextBox1.AppendText(glyph); 

dear Jalal, as I underestand You have a String (in file or within a TextBox) when You are parsing the string from the Box everything goes fine but when You are trying to read from a file you face with problem.

You have a XML document that You have strore the Characters Glyph in that. and each element of this XML (eg: Item) has some attribute like Glyph. (the shape of character)

If You only want to display the Shape of the Character You're in serios wrong way. the only thing You should do is that write a simple method that gives a Decimal (or hex) value of character and return the Character.

Returned Character will display everywhere as a single character. so I strongly recommand ou to change the method ( this will speed up you app ).

// het a hex and return char (you can give it a large string or a single hexcode
// (hex without U just HexCode)
public static char ConvertHexToUnicode(string hexCode)
    {
        if (hexCode != string.Empty)
            return ((char)int.Parse(hexCode, NumberStyles.AllowHexSpecifier));

        char empty = new char();
        return empty;
    }//end

and for decimal value use the following code

 public static char ConvertDecimalToChar(Int64 decimalValue)
    {
        return ((char)int.Parse(decimalValue.ToString(), NumberStyles.Integer));
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM