开发者

how to convert string of unicodes to the char?

开发者 https://www.devze.com 2023-03-02 01:30 出处:网络
i have a text file in which sets of Unicodes are written as \"\'\\u0641\'\",\"\'\\u064A\',\'\\u0649\',\'\\u0642\',\'\\u0625\',\'\\u0644\',\'\\u0627\',\'\\u0647\',\'\\u0631\',\'\\u062A\',\'\\u0643\',\

i have a text file in which sets of Unicodes are written as

"'\u0641'","'\u064A','\u0649','\u0642','\u0625','\u0644','\u0627','\u0647','\u0631','\u062A','\u0643','\u0645','\u0639','\u0648','\u0623','\u0646','\u0636','\u0635','\u0633','\u0641','\u062D','\u0628','\u0650','\u064E','\u062C','\u0626" "'\u0622'","'\u062E','\u0644','\u064A','\u0645".

I opened the file and started reading of file by using readline method. I got the above line shown as a line now i want to convert all Unicode to char so that i could get a readable string. i tried some logic but that doesn't worked i stuck with converting string "'开发者_运维技巧\u00641'" to char.


You can extract strings containing individual numbers (using Regex for example), apply Int16.Parse to each and then convert it to a char.

string num = "0641"; // replace it with extracting logic of your preference
char c = (char)Int16.Parse(num, System.Globalization.NumberStyles.HexNumber);


You could parse the line to get each unicode char. To convert unicode to readable character you could do

char MyChar = '\u0058';

Hope this help


What if you do something like this:

string codePoints = "\u0641 \u064A \u0649 \u0642 \u0625";

UnicodeEncoding uEnc = new UnicodeEncoding();

byte[] bytesToWrite = uEnc.GetBytes(codePoints);
System.IO.File.WriteAllBytes(@"yadda.txt", bytesToWrite);


byte[] readBytes = System.IO.File.ReadAllBytes(@"yadda.txt");
string val = uEnc.GetString(readBytes);

//daniel

0

精彩评论

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