前几天帮同事搞一个问题,用.NET程序调Oracle数据库,数据库的默认编码方式是WE8MSWIN1252,用.NET读出来是乱码.
由于Oracle是别人的数据库,不能修改,所以只能在程序中进行编码转换,翻发很多资料发现WE8MSWIN1252对应是Windows-1252,
编码转换的方法如下:
1
public
static
string
GetEncoding(
string
tmp)
2![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
3
Encoding win1252 = UnicodeEncoding.GetEncoding("windows-1252");
4
Encoding gb2312 = UnicodeEncoding.GetEncoding("gb2312");
5
byte[] bytes = win1252.GetBytes(tmp);
6
char[] outChar = gb2312.GetChars(bytes);
7
return new String(outChar);
8
}
![None.gif](/Images/OutliningIndicators/None.gif)
2
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](/Images/OutliningIndicators/ContractedBlock.gif)
![dot.gif](/Images/dot.gif)
3
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
4
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
5
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
6
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
7
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
8
![ExpandedBlockEnd.gif](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
希望能帮到遇到类似问题的朋友.^_^