Convert UTF-8 to Chinese Simplified (GB2312)
2 answers
The first thing to know is that there is no such thing as "UTF-8 string" in .NET. All strings in .NET are effectively UTF-16. However, .NET provides a class Encoding
that allows you to decode binary data into strings and re-encode it later.
Encoding.Convert
can convert a byte array representing text encoded with one encoding to an array of bytes with the same text encoded with a different encoding. Is this what you want?
Also, if you already have a string, you can use:
byte[] bytes = Encoding.GetEncoding("gb2312").GetBytes(text);
If you can provide more information, it would be helpful.
+9
a source to share
Try it;
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;
}
0
a source to share