当前位置:首页 > C# GB2312 和 UTF8 中文编码转换

C# GB2312 和 UTF8 中文编码转换

点击次数:5152  更新日期:2011-12-16
public string GB2312ToUtf8(string gb2312String)
{
    Encoding fromEncoding = Encoding.GetEncoding("gb2312");
    Encoding toEncoding = Encoding.UTF8;
    return EncodingConvert(gb2312String, fromEncoding, toEncoding);
}


public string Utf8ToGB2312(string utf8String)
{
    Encoding fromEncoding = Encoding.UTF8;
    Encoding toEncoding = Encoding.GetEncoding("gb2312");
    return EncodingConvert(utf8String, fromEncoding, toEncoding);
}


public string EncodingConvert(string fromString, Encoding fromEncoding, Encoding toEncoding)
{            
    byte[] fromBytes = fromEncoding.GetBytes(fromString);
    byte[] toBytes = Encoding.Convert(fromEncoding, toEncoding, fromBytes);
    
    string toString = toEncoding.GetString(toBytes);
    return toString;
}