UTF8转GB2312
公司有个项目需要跟别的系统互通,新做的是utf-8编码的,另一个是gb2312的,需要把utf-8编码转为gb2312。写了下面的代码:
最后没办法,把新做的这一页单独转成了gb2312的,在load里添加:
if (!IsPostBack)
Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
各位高人帮忙看看上面的那段代码怎么不成功呢?
public string UTF8toGB2312(string str)
{
string utfStr = str;
byte[] utfBytes = System.Text.Encoding.UTF8.GetBytes(utfStr);
byte[] gbBytes = System.Text.Encoding.Convert(System.Text.Encoding.UTF8,System.Text.Encoding.GetEncoding("gb2312"),utfBytes);
char[] asciiChars = new char[System.Text.Encoding.GetEncoding("gb2312").GetCharCount(gbBytes, 0, gbBytes.Length)];
System.Text.Encoding.GetEncoding("gb2312").GetChars(gbBytes, 0, gbBytes.Length, asciiChars, 0);
string gbStr = new string(asciiChars);
return gbStr;
}
可惜转来转去不成功啊,郁闷了~{
string utfStr = str;
byte[] utfBytes = System.Text.Encoding.UTF8.GetBytes(utfStr);
byte[] gbBytes = System.Text.Encoding.Convert(System.Text.Encoding.UTF8,System.Text.Encoding.GetEncoding("gb2312"),utfBytes);
char[] asciiChars = new char[System.Text.Encoding.GetEncoding("gb2312").GetCharCount(gbBytes, 0, gbBytes.Length)];
System.Text.Encoding.GetEncoding("gb2312").GetChars(gbBytes, 0, gbBytes.Length, asciiChars, 0);
string gbStr = new string(asciiChars);
return gbStr;
}
最后没办法,把新做的这一页单独转成了gb2312的,在load里添加:
if (!IsPostBack)
Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
各位高人帮忙看看上面的那段代码怎么不成功呢?