中文字符串转换成十六进制的网址格式
使用中文unicode作网址,第三方控件出现乱码。在我自己的电脑上测试没问题的,到别的机器就有问题了。
一个非常诡异的问题。
估计是编码不一致引起的。
干脆直接把中文转换成了十六进制。
- /// <summary>
- /// 中文字符串转换成十六进制的网址格式
- /// </summary>
- /// <param name="str"></param>
- /// <returns></returns>
- public static string String2Hex(string str)
- {
- //把字符串放到数组里
- char[] c = new char[str.Length];
- for (int i = 0; i < str.Length; i++)
- {
- c[i] = str[i];
- }
- //注意编码,根据需要GB2312或UTF-8
- Byte[] textByte = System.Text.Encoding.GetEncoding("UTF-8").GetBytes(c);
- StringBuilder text = new StringBuilder();
- for (int j = 0; j < textByte.Length; j++)
- {
- // to char
- char textChar = Convert.ToChar(int.Parse(textByte[j].ToString()));
- // to Hex
- text.Append(System.Uri.HexEscape(textChar));
- }
- return text.ToString();
- }








