问题
I have base64 encoded string, result being in iso-8859-2 encoding and I need to get the string from it.
I'm woking on windows Phone 8 C#/XAML .NET 4.5 App.
What I'm doing right now is:
private string decodeXML(string xml)
{
byte[] data = Convert.FromBase64String(xml); //I decode the base64 data
string decoded = Encoding.UTF8.GetString(data, 0, data.Length);
//and here get a string from it using UTF8, problem being the data are in "iso-8859-2"
return decoded;
}
So I tried:
private string decodeXML(string xml)
{
byte[] data = Convert.FromBase64String(xml);
string decoded = Encoding.GetEncoding("iso-8859-2").GetString(data, 0, data.Length);
return decoded;
}
But I get an exception
System.ArgumentException was unhandled by user code
HResult=-2147024809
Message='iso-8859-2' is not a supported encoding name.
Parameter name: name
Source=mscorlib
ParamName=name
StackTrace:
at System.Globalization.EncodingTable.internalGetCodePageFromName(String name)
at System.Globalization.EncodingTable.GetCodePageFromName(String name)
at System.Text.Encoding.GetEncoding(String name)
.....and the rest of the stack trace...
How should I get the string from it with special characters preserved? (which way to use to get the string from it?)
回答1:
Here is the code for manual conversion from ISO 8859-2 to String. Not tested:
private readonly ushort[] iso8859_2 = {
0x00A0, 0x0104, 0x02D8, 0x0141, 0x00A4, 0x013D, 0x015A, 0x00A7, 0x00A8, 0x0160,
0x015E, 0x0164, 0x0179, 0x00AD, 0x017D, 0x017B, 0x00B0, 0x0105, 0x02DB, 0x0142,
0x00B4, 0x013E, 0x015B, 0x02C7, 0x00B8, 0x0161, 0x015F, 0x0165, 0x017A, 0x02DD,
0x017E, 0x017C, 0x0154, 0x00C1, 0x00C2, 0x0102, 0x00C4, 0x0139, 0x0106, 0x00C7,
0x010C, 0x00C9, 0x0118, 0x00CB, 0x011A, 0x00CD, 0x00CE, 0x010E, 0x0110, 0x0143,
0x0147, 0x00D3, 0x00D4, 0x0150, 0x00D6, 0x00D7, 0x0158, 0x016E, 0x00DA, 0x0170,
0x00DC, 0x00DD, 0x0162, 0x00DF, 0x0155, 0x00E1, 0x00E2, 0x0103, 0x00E4, 0x013A,
0x0107, 0x00E7, 0x010D, 0x00E9, 0x0119, 0x00EB, 0x011B, 0x00ED, 0x00EE, 0x010F,
0x0111, 0x0144, 0x0148, 0x00F3, 0x00F4, 0x0151, 0x00F6, 0x00F7, 0x0159, 0x016F,
0x00FA, 0x0171, 0x00FC, 0x00FD, 0x0163, 0x02D9 };
private string decodeXML(string xml)
{
byte[] data = Convert.FromBase64String(xml);
char[] decoded = new char[data.Length];
for (int i = 0; i < data.Length; i++)
if (data[i] < 128)
decoded[i] = (char)data[i];
else if (data[i] < 0xA0)
decoded[i] = '\0';
else
decoded[i] = (char)iso8859_2[data[i] - 0xA0];
return new string(decoded);
}
回答2:
Encoding.GetEncoding("iso-8859-2") works for me, at least in .NET 4.5. So does Encoding.GetEncoding("latin2"). Their codepage is 28592, so Encoding.GetEncoding(28592) gives the same result. The windows codepage 1250 is also similar (or exactly the same?), so you can try Encoding.GetEncoding(1250) or Encoding.GetEncoding("windows-1250").
Update: oh yes, WP8... See this: 'windows-1255' is not a supported encoding name . So basically it may not be supported at all and you should do a manual mapping of bytes to characters based on the specs of the encoding. It's not that hard, create a char array of size 256, and array[i] should be the character that corresponds to byte i in that encoding.
来源:https://stackoverflow.com/questions/20139281/windows-phone-8-getting-string-from-base64-iso-8859-2-encoded-text