Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert string encoded in windows-1250 to unicode?

Tags:

c#

unicode

I am receiving from some dll (which is wrapper for some external data source) strings in Windows-1250 codepage and I would like to insert them correctly (as unicode) to table in SQL Server Database. Since particular row in database which should hold that data is of NVarchar type, I only needed to convert it in my C# code to unicode and pass it as parameter. Everything is well and nice, but I stumbled on conversion step.

I tried the following but that doesn't work:

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);
}

When I insert this returned string to table, I don't get correct letters like Đ, đ, Č, č, Ć or ć.

Please, help! :)

like image 830
Draško Avatar asked Apr 23 '10 14:04

Draško


1 Answers

the problem is string string2Encode is in unicode already. (UTF-16 is internal encoding in .net) So if you want string in windows-1250, you have to pass byte[] then do Encoding.GetEncoding("windows-1250").GetString()

like image 180
Andrey Avatar answered Sep 21 '22 05:09

Andrey