有关字符编码的问题,如果将"%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()方法好像不行。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 System.Web.HttpUtility.UrlDecode("%C4%E3%BA%C3%B0%A1%A3%A1",System.Text.Encoding.Default); byte[] bytes = System.Text.Encoding.Default.GetBytes(code); string decode = System.Text.Encoding.UTF8.GetString(bytes); 给你一个方法,看这个方法,你可以了解到编码过程public static string FromUtf8(string str) { if (str == null) { return string.Empty; } else { StringBuilder sb = new StringBuilder(); for (int i = 0; i < str.Length; ++i) { if (i < (str.Length - 2)) { if (str.Substring(i, 3).Equals("%3A")) { sb.Append(':'); i++; i++; } else if (str.Substring(i, 3).Equals("%2E")) { sb.Append('.'); i++; i++; } else if (str.Substring(i, 3).Equals("%2F")) { sb.Append('/'); i++; i++; } else { sb.Append(str[i]); } } else { sb.Append(str[i]); } } return sb.ToString(); } } Sydney_Carton(悉尼盒子) hanmlxiao(hanml)的方法都不行,至于zhchg6666,这段程序难道就只能够解码几个标点符号吗?另外,那个HttpUtility是不能用在WinForm程序中的。请大家回复的时候自己先测试一下!其实我想比较苯的方法就是将每一个%XX转换为十进制数字,然后再用GetString方法还原字符串。不过我想.NET平台应该有现成的方法吧?自己写有点不太合适。 个人感觉%C4%E3%BA%C3%B0%A1%A3%A1之类是urlencode后的结果,urldecode结果是"你好啊!"另外httputility是可以用在winform下的,不过你得引用System.Web.dll 请问楼上的如何引用System.Web.dll? 接受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; } } byteList.Add((byte)num); } } return Encoding.UTF8.GetString(byteList.ToArray()); }经过测试,可以来回转换字符串和utf8 zhchg6666() 你好强!衷心感谢你的工作! 这段代码为什么只能删除一行呢? timestamp 问题 c#执行数据库操作时间太长,程序假死 谁用过socket传送xml文件?高手请进! TCPChannel这个类怎么在名称空间下没有!! 现在有什么办法可以通过程序给qq或msn发消息,谢谢 急呀!请高人解答 我是asp.net区的/这两天做一个东东.问一下从一个winform窗体跳到别一个winform窗体里如何做////这个问题太无奈 C#如何在低版本的fastreport的条码控件添加QRcode选项的条码类型 大家帮忙呀 真诚求助绑定的问题?(combobox,checkbox) C#程序调试报错->难得一见的报错
string decode = System.Text.Encoding.UTF8.GetString(bytes);
public static string FromUtf8(string str)
{
if (str == null)
{
return string.Empty;
}
else
{ StringBuilder sb = new StringBuilder();
for (int i = 0; i < str.Length; ++i)
{
if (i < (str.Length - 2))
{
if (str.Substring(i, 3).Equals("%3A"))
{
sb.Append(':'); i++;
i++;
}
else if (str.Substring(i, 3).Equals("%2E"))
{
sb.Append('.');
i++;
i++;
}
else if (str.Substring(i, 3).Equals("%2F"))
{
sb.Append('/');
i++;
i++;
}
else
{
sb.Append(str[i]);
}
}
else
{
sb.Append(str[i]);
}
}
return sb.ToString();
}
}
hanmlxiao(hanml)的方法都不行,至于zhchg6666,这段程序难道就只能够解码几个标点符号吗?
另外,那个HttpUtility是不能用在WinForm程序中的。
请大家回复的时候自己先测试一下!其实我想比较苯的方法就是将每一个%XX转换为十进制数字,然后再用GetString方法还原字符串。不过我想.NET平台应该有现成的方法吧?自己写有点不太合适。
另外httputility是可以用在winform下的,不过你得引用System.Web.dll
{
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;
}
} byteList.Add((byte)num);
}
} return Encoding.UTF8.GetString(byteList.ToArray());
}经过测试,可以来回转换字符串和utf8
衷心感谢你的工作!