开发者

Unicode rendering in c#

开发者 https://www.devze.com 2022-12-17 00:10 出处:网络
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.

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, \uFB80) 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 \uFB80 or trying to add a "\u" in front of the value bot开发者_JAVA百科h results in the system reading it as \uFB80 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 (e.g: 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));
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号