Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
407 views
in Technique[技术] by (71.8m points)

sql server - Cyrillic encoding in C#

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

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

1 Reply

0 votes
by (71.8m points)
// 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: Работа в германии

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...