从unicode转换为原始格式C# [英] Conversion from unicode to original format C#
问题描述
我想将Unicode转换为原始格式UTF8,我认为它可能是阿拉伯语,如下所示。用户键入一些unicode文本,当他点击转换时,它将转换为原始格式,如下所示:
I want to convert from Unicode to its original format UTF8 I think and it could be arabic like below. The user types some unicode text and when he clicks on convert it'll be converted to the original format like below :
string unicode ="\u0633\u0637\u0648\u0631 \u0639\u0628\u0631 \u0627\u0644\u0623\u064a\u0627\u0645 1";
输出必须等于原始格式:
سطورعبرالأيام1 | شمسالدينخ
i used http://msdn.microsoft.com/en-us/library/vstudio/kdcak6ye%28v=vs.100%29.aspx [ ^ ]
作为对我的参考,但输出是一样的输入没有发生没有转换已发生任何人都可以告诉我什么是错的?
我的尝试:
output must equal the original format:
سطور عبر الأيام 1 | شمس الدين خ
i used http://msdn.microsoft.com/en-us/library/vstudio/kdcak6ye%28v=vs.100%29.aspx[^]
as a reference to me but the output is the same like the input nothing happened no conversion has been happened could anyone advice me what's wrong?
What I have tried:
private void btn_convert_Click(object sender, EventArgs e)
{
string unicodestring = txt_unicode.Text;
// Create two different encodings.
Encoding ascii = Encoding.ASCII;
Encoding unicode = Encoding.Unicode;
//Encoding Utf8 = Encoding.UTF8;
// // Convert the string into a byte array.
byte[] unicodeBytes = unicode.GetBytes(unicodestring);
// // Perform the conversion from one encoding to the other.
byte[] ascibytes = Encoding.Convert(unicode,ascii,unicodeBytes);
// // Convert the new byte[] into a char[] and then into a string.
char[] asciiChars = new char[ascii.GetCharCount(ascibytes, 0, ascibytes.Length)];
ascii.GetChars(ascibytes, 0, ascibytes.Length, asciiChars, 0);
string asciiString = new string(asciiChars);
// // Display the strings created before and after the conversion.
//MessageBox.Show("Original string is"+unicodeString);
MessageBox.Show("Ascii converted string is" + asciiString);
}
推荐答案
您可以使用此功能:
System.Uri.UnescapeDataString(string)
我为你测试:
I test it for you:
string unicode = "\u0633\u0637\u0648\u0631 \u0639\u0628\u0631 \u0627\u0644\u0623\u064a\u0627\u0645 1";
string str = System.Uri.UnescapeDataString(unicode);
结果( str 内容)是:
سطورعبرالأيام1
result (str content) is:
سطور عبر الأيام 1
只需使用:
Simply use:
var output = System.Net.WebUtility.HtmlDecode(text);
最后我得到解决方案,如果有人需要一些参考
Finally i get the solution if anyone need some reference
private string convert_from_unicode(string str,char c)
{
string rtstr = "";
for (int i=2;i< str.Length; i+=6)
{
string str1 = str.Substring(i, 4);
c = (char)Int16.Parse(str1, System.Globalization.NumberStyles.HexNumber);
rtstr += c;
}
return rtstr;
}
private void btn_convert_Click(object sender, EventArgs e)
{
char f = ' ';
txt_result.Text = convert_from_unicode(txt_unicode.Text, f);
}
这篇关于从unicode转换为原始格式C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!