A
Albert Jan
Hi,
I have text from mime email messages with different encoding that I want to
convert to utf-8, but I'm relatively new on encoding problems.
I use the following code, but this doesn't seem to work (the in and output
remains the same):
string
x=toUTF8("=?GB2312?B?s8q5q8u+vq3A7aGissbO8bK/w8W1xNK7t+LQxQ==?=","GB2312");
public static string toUTF8(string messageString, string charset)
{
Encoding dstEnc = Encoding.UTF8;
if(charset.Length==0)
{
charset="us-ascii";
}
Encoding srcEnc=Encoding.GetEncoding(charset);
byte[] srcData = srcEnc.GetBytes( messageString );
byte[] dstData;
// see if we need to convert data
if(dstEnc != srcEnc )
{
dstData = Encoding.Convert( srcEnc, dstEnc, srcData );
}
else
{
dstData = srcData;
}
char[] utf8Chars = new char[Encoding.UTF8.GetCharCount(dstData, 0,
dstData.Length)];
Encoding.UTF8.GetChars(dstData, 0, dstData.Length, utf8Chars, 0);
string utf8String = new string(utf8Chars);
return utf8String;
}
Any help would be appreciated,
Albert Jan
I have text from mime email messages with different encoding that I want to
convert to utf-8, but I'm relatively new on encoding problems.
I use the following code, but this doesn't seem to work (the in and output
remains the same):
string
x=toUTF8("=?GB2312?B?s8q5q8u+vq3A7aGissbO8bK/w8W1xNK7t+LQxQ==?=","GB2312");
public static string toUTF8(string messageString, string charset)
{
Encoding dstEnc = Encoding.UTF8;
if(charset.Length==0)
{
charset="us-ascii";
}
Encoding srcEnc=Encoding.GetEncoding(charset);
byte[] srcData = srcEnc.GetBytes( messageString );
byte[] dstData;
// see if we need to convert data
if(dstEnc != srcEnc )
{
dstData = Encoding.Convert( srcEnc, dstEnc, srcData );
}
else
{
dstData = srcData;
}
char[] utf8Chars = new char[Encoding.UTF8.GetCharCount(dstData, 0,
dstData.Length)];
Encoding.UTF8.GetChars(dstData, 0, dstData.Length, utf8Chars, 0);
string utf8String = new string(utf8Chars);
return utf8String;
}
Any help would be appreciated,
Albert Jan