private
string
Convert(String str)
{
char[] hexDigits = { '0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
System.Text.Encoding utf8 = System.Text.Encoding.GetEncoding("GB2312");
System.Text.StringBuilder result = new System.Text.StringBuilder();
for (int i = 0; i < str.Length; i++)
{
string sub = str.Substring(i, 1);
byte[] bytes = utf8.GetBytes(sub);
if (bytes.Length == 1) //普通英文字母或数字
{
result.Append(sub);
}
else //其它字符,转换成为编码
{
for (int j = 0; j < bytes.Length; j++)
{
result.Append("%" + hexDigits[bytes[j] >> 4] + hexDigits[bytes[j] & 0XF]);
}
}
}
return result.ToString();
}
例如在设置为System.Text.Encoding.GetEncoding("GB2312")时可以将"自贡"转换为"%D7%D4%B9%B1"的gb2312编码~感觉比较实用
{
char[] hexDigits = { '0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
System.Text.Encoding utf8 = System.Text.Encoding.GetEncoding("GB2312");
System.Text.StringBuilder result = new System.Text.StringBuilder();
for (int i = 0; i < str.Length; i++)
{
string sub = str.Substring(i, 1);
byte[] bytes = utf8.GetBytes(sub);
if (bytes.Length == 1) //普通英文字母或数字
{
result.Append(sub);
}
else //其它字符,转换成为编码
{
for (int j = 0; j < bytes.Length; j++)
{
result.Append("%" + hexDigits[bytes[j] >> 4] + hexDigits[bytes[j] & 0XF]);
}
}
}
return result.ToString();
}