我从Windows-1250代码页中的一些dll(它是一些外部数据源的包装)字符串接收,我想将它们正确地插入(作为unicode)到SQL Server数据库中的表.由于数据库中应该保存该数据的特定行是NVarchar类型,我只需要将其在我的C#代码中转换为unicode并将其作为参数传递.一切都很好,但我偶然发现转换步骤.
我尝试了以下但不起作用:
private static String getUnicodeValue(string string2Encode) //
{
Encoding srcEncoding = Encoding.GetEncoding("Windows-1250");
UnicodeEncoding dstEncoding = new UnicodeEncoding();
byte[] srcBytes = srcEncoding.GetBytes(string2Encode);
byte[] dstBytes = dstEncoding.GetBytes(string2Encode);
return dstEncoding.GetString(dstBytes);
}
Run Code Online (Sandbox Code Playgroud)
当我将这个返回的字符串插入表格时,我没有得到正确的字母,如Đ,đ,Č,č,Ć或ć.
请帮忙!:)
And*_*rey 14
问题是string string2Encode
已经在unicode.(UTF-16是.net中的内部编码)所以如果你想在windows-1250中使用字符串,你必须通过byte[]
然后再做Encoding.GetEncoding("windows-1250").GetString()