Adi
Adi

Reputation: 21

convert unicode(hex) to string in C#

Is there any approach to convert Unicode like "U+4E0B" to its equivalent character?

Ex- {\U+FF8D\U+FF9E\U+FF9D\U+FF84\U+FF9E\U+FF97\U+FF72\U+FF9D - \U+4E0B}

any type of help is appreciated!

Upvotes: 2

Views: 1150

Answers (1)

xanatos
xanatos

Reputation: 111850

Simple way is using a regex + Regex.Replace() with delegate:

string str = @"\U+FF8D\U+FF9E\U+FF9D\U+FF84\U+FF9E\U+FF97\U+FF72\U+FF9D - \U+4E0BFooBar";

var rx = new Regex(@"\\U\+([0-9A-F]{4})");

string str2 = rx.Replace(str, m =>
{
    ushort u = Convert.ToUInt16(m.Groups[1].Value, 16);
    return ((char)u).ToString();
});

Unclear if you want to be case insensitive (so that \u+ff9e is valid), then use:

var rx = new Regex(@"\\[Uu]\+([0-9A-Fa-f]{4})");

Upvotes: 3

Related Questions