请问GBK编码的一个问题,把已转的转成汉字

renmms 2012-03-25 01:58:34
比如: %C4%E3%BA%C3 是"你好”的意思。


怎么把 %C4%E3%BA%C3 转成 “你好" ?

多谢!
...全文
134 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
renmms 2012-03-25
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 fangxinggood 的回复:]

刚才在SL4试了下:

C# code
public static string UrlDecode(string input)
{
var arr = input.Split(new[] {'%'}, StringSplitOptions.RemoveEmptyEntries);
var bytes = arr.Select(s => Convert.ToByte……
[/Quote]

多谢了!这个可以。

看代码的话,首先吧%去掉,然后对于每一个字符比如说'C4‘做Convert.ToByte(s, 16)操作,是因为中文是16位的是把?
机器人 2012-03-25
  • 打赏
  • 举报
回复
刚才在SL4试了下:

 public static string UrlDecode(string input)
{
var arr = input.Split(new[] {'%'}, StringSplitOptions.RemoveEmptyEntries);
var bytes = arr.Select(s => Convert.ToByte(s, 16)).ToArray();
var result = Encoding.GetEncoding("GB2312").GetString(bytes, 0, bytes.Length);
return result;
}


很可惜,SL4也还不支持 GB2312 或者 GBK 编码。
renmms 2012-03-25
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 fangxinggood 的回复:]

你不是说要 Encode 嘛? "对了,我想要的是Encode 而不是Decode"


private static bool IsReverseChar(char c)
{
return !((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && ……
[/Quote]

不好意思.我上面好像说错了。

我就是想通过 “%C4%E3%BA%C3” 得到 你好 2个字
机器人 2012-03-25
  • 打赏
  • 举报
回复
你不是说要 Encode 嘛? "对了,我想要的是Encode 而不是Decode"


private static bool IsReverseChar(char c)
{
return !((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9')
|| c == '-' || c == '_' || c == '.' || c == '~');
}
renmms 2012-03-25
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 fangxinggood 的回复:]

[/Quote]

你好,你的方法也不行,而且少了一个方法 IsReverseChar()

我就是想通过 “%C4%E3%BA%C3” 得到 你好 2个字
机器人 2012-03-25
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 renmms 的回复:]

引用 1 楼 fangxinggood 的回复:

var str = System.Web.HttpUtility.UrlDecode("%C4%E3%BA%C3", System.Text.Encoding.GetEncoding("GB2312"));

添加 System.Web.dll


对了,我想要的是Encode 而不是Decode
[/Quote]

可以直接用下面的方法代替,Encoding.UTF8 自己根据实际替换
public static string UrlEncode(string input)
{
StringBuilder newBytes = new StringBuilder();
var urf8Bytes = Encoding.UTF8.GetBytes(input);
foreach (var item in urf8Bytes)
{
if (IsReverseChar((char)item))
{
newBytes.Append('%');
newBytes.Append(item.ToString("X2"));

}
else
newBytes.Append((char)item);
}

return newBytes.ToString();
}


renmms 2012-03-25
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 fangxinggood 的回复:]

var str = System.Web.HttpUtility.UrlDecode("%C4%E3%BA%C3", System.Text.Encoding.GetEncoding("GB2312"));

添加 System.Web.dll
[/Quote]

对了,我想要的是Encode 而不是Decode
porschev 2012-03-25
  • 打赏
  • 举报
回复

string aaa = HttpUtility.UrlDecode(str, System.Text.Encoding.GetEncoding("GBK"));
renmms 2012-03-25
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 fangxinggood 的回复:]

var str = System.Web.HttpUtility.UrlDecode("%C4%E3%BA%C3", System.Text.Encoding.GetEncoding("GB2312"));

添加 System.Web.dll
[/Quote]

多谢回复。

除了 HttpUtility.UrlDecode 方法还有其他办法么?

我是在silverlight里使用, SL里HttpUtility.UrlDecode()只有一个参数,没有Encording参数。
机器人 2012-03-25
  • 打赏
  • 举报
回复
var str = System.Web.HttpUtility.UrlDecode("%C4%E3%BA%C3", System.Text.Encoding.GetEncoding("GB2312"));

添加 System.Web.dll

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧