Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将Unicode字符串转换为字符?_C#_Windows_Silverlight_Windows Phone 7 - Fatal编程技术网

C# 如何将Unicode字符串转换为字符?

C# 如何将Unicode字符串转换为字符?,c#,windows,silverlight,windows-phone-7,C#,Windows,Silverlight,Windows Phone 7,我有一个文本文件,其中一组Unicode被写为 “'\u0641'”、'\u064A'、'\u0649'、'\u0642'、'\u0625'、'\u0644'、'\u0627'、'\u0647'、'\u0631'、'\u062A'、'\u0643'、'\u0645'、'\u0639'、'\u0648'、'\u0623'、'\u0646'、'\u0636'、'\u0635'、'\u0633'、'\u0641'、'\u0628'、\u0625'、\u0625'、\u0625'、\u0625'、' “

我有一个文本文件,其中一组Unicode被写为

“'\u0641'”、'\u064A'、'\u0649'、'\u0642'、'\u0625'、'\u0644'、'\u0627'、'\u0647'、'\u0631'、'\u062A'、'\u0643'、'\u0645'、'\u0639'、'\u0648'、'\u0623'、'\u0646'、'\u0636'、'\u0635'、'\u0633'、'\u0641'、'\u0628'、\u0625'、\u0625'、\u0625'、\u0625'、' “'\u0622'”、“\u062E'、“\u0644'、“\u064A'、“\u0645”


我打开了文件并开始使用readline方法读取文件。我把上面的一行显示为一行,现在我想把所有的Unicode转换成char,这样我就可以得到一个可读的字符串。我尝试了一些逻辑,但没有成功,我坚持将字符串“'\u00641'”转换为字符

您可以提取包含单个数字的字符串(例如使用Regex),对每个字符串应用Int16.Parse,然后将其转换为字符

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

您可以解析该行以获得每个unicode字符。要将unicode转换为可读字符,可以执行以下操作

char MyChar = '\u0058';

希望这对您有所帮助

如果您这样做会怎么样:

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

您从哪里获取此文件?更改创建它的内容可能比解析它更简单。我们在项目中使用这些文件。我不明白你想说什么但是文件是从哪里来的?字符可以隐式转换为ushort、int、uint、long、ulong、float、double或decimal。(来自MSDN)您可以编写:ushort code=c;变量代码将保存一个unicode值c。如果要将其编码为类似于输入文件的字符串,则应编写:string.Format(“\\u{0:X4}”,(ushort)c);