有关字符编码的问题,如果将"%C4%E3%BA%C3%B0%A1%A3%A1"之类的字符转换为正常的字符?
我用WebRequest类向一个http服务器获取信息,服务器返回给我的是一些类似:
%C4%E3%BA%C3%B0%A1%A3%A1%2B_-)(*%26%5E
这样的信息,我知道这个字符是经过unicode编码的,例如最后的一个%5E其实就代表“^”字符。
用UnicodeEncoding.GetString()方法好像不行。。。。
------解决方案--------------------接受aluzi的批评 重新写了一个解析utf8的函数
private void button2_Click(object sender, EventArgs e)
{
string a = "你好啊! ";
a = ToUtf8(a);
a = FromUtf8(a);
textBox1.Text = a;
}
//用于将字符串转换成UTF-8编码
private static string ToUtf8(string str)
{
if (str == null)
{
return string.Empty;
}
else
{
char[] hexDigits = { '0 ', '1 ', '2 ', '3 ', '4 ',
'5 ', '6 ', '7 ', '8 ', '9 ',
'A ', 'B ', 'C ', 'D ', 'E ', 'F '};
Encoding utf8 = Encoding.UTF8;
StringBuilder result = new StringBuilder();
for (int i = 0; i < str.Length; i++)
{
string sub = str.Substring(i, 1);
byte[] bytes = utf8.GetBytes(sub);
for (int j = 0; j < bytes.Length; j++)
{
result.Append( "% " + hexDigits[bytes[j] > > 4] + hexDigits[bytes[j] & 0XF]);
}
}
return result.ToString();
}
}
//将UTF-8编码转换成字符串
public string FromUtf8(string str)
{
char[] hexDigits = { '0 ', '1 ', '2 ', '3 ', '4 ',
'5 ', '6 ', '7 ', '8 ', '9 ',
'A ', 'B ', 'C ', 'D ', 'E ', 'F '};
List <byte> byteList = new List <byte> (str.Length / 3);
if (str != null)
{
List <string> strList = new List <string> ();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < str.Length; ++i)
{
if (str[i] == '% ')
{
strList.Add(str.Substring(i, 3));
}
}
foreach (string tempStr in strList)
{
int num = 0;
int temp = 0;
for (int j = 0; j < hexDigits.Length; ++j)
{
if (hexDigits[j].Equals(tempStr[1]))
{
temp = j ;
num = temp < < 4;
}
}
for (int j = 0; j < hexDigits.Length; ++j)
{
if (hexDigits[j].Equals(tempStr[2]))
{
num += j;
}
}