Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a bunch of Cyrillic-like text in a MSSQL database and need to convert it to Cyrillic in C#.

So... Dàá?òà a ??eìàíèè

should become

Работа в германии

Any suggestions?

I should add that the closest I've gotten is ?aaioa a aa?iaiee

Here's the code I'm using:

 str = Encoding.UTF8.GetString(Encoding.GetEncoding("Windows-1251").GetBytes(drCurrent["myfield"].ToString()));
 str = Encoding.GetEncoding(1251).GetString(Encoding.Convert(Encoding.UTF8, Encoding.GetEncoding(1251), Encoding.UTF8.GetBytes(str)));
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
392 views
Welcome To Ask or Share your Answers For Others

1 Answer

// To find out source and target
const string source = "Dàá?òà a ??eìàíèè";
const string destination = "Работа в германии";

foreach (var sourceEncoding in Encoding.GetEncodings())
{

    var bytes = sourceEncoding.GetEncoding().GetBytes(source);
    foreach (var targetEncoding in Encoding.GetEncodings())
    {
        if (targetEncoding.GetEncoding().GetString(bytes) == destination)
        {
            Console.WriteLine("Source Encoding: {0} TargetEncoding: {1}",sourceEncoding.CodePage,targetEncoding.CodePage);
        }

    }
}

// Result1: Source Encoding: 1252 TargetEncoding: 1251
// Result2: Source Encoding: 28591 TargetEncoding: 1251
// Result3: Source Encoding: 28605 TargetEncoding: 1251

// The code for you to use 
var decodedCyrillic = Encoding.GetEncoding(1251).GetString(Encoding.GetEncoding(1252).GetBytes(source));
// Result: Работа в германии

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...