开发者

Woes with unrecognised escape sequence with unicode characters

开发者 https://www.devze.com 2023-04-10 11:06 出处:网络
I\'m writing a program. Part of this program involves reading in a unicode value from an XML file and displaying the character on screen.

I'm writing a program. Part of this program involves reading in a unicode value from an XML file and displaying the character on screen.

Now when I did it like this:

tbTester.Text = "\u597D";

It worked fine (tbTester is a Winforms text box). But with the other situation, basically I needed to alter a string to have the '\u' and then the value. Like this:

szOut = szOut + "\u"+k.UnicodeID + " ";

For me, these don't look all that different. Only now it tells me that the "\u" is an unrecognis开发者_StackOverflowed escape sequence.

Now I did look this problem up and a double slash or the '@' symbol does cure this particular situation, only now the text box contains '\u430B' (or whatever) rather than the character which was output in the first of my examples.


When you compile code like your first example, the compiled CIL code doesn't actually contain the escape sequence, but the character itself. And because \u is invalid by itself, this causes the error you're getting.

If you have have Unicode code point as an integer, you can convert it into a character simply by casting. And the + operator will take care of the rest:

szOut = szOut + (char)k.UnicodeID + " ";

Although I tend to prefer string.Format() in cases like this:

szOut = string.Format("{0}{1} ", szOut, (char)k.UnicodeID);


Try

szOut = szOut + (char)k.UnicodeID;

You don't need to escape the character anymore since it's not in a literal string. You just need to convert the number to it's character equivelant.

0

精彩评论

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

关注公众号