There is a specific code that receives and displays a listing from the database (MySQL).

I can not overcome one column, there the data (text) lie in cp1163 encoding.

Accordingly, the listing displays krakozyabry.

Connoisseurs, please tell me how to decode the resulting cp1251 using standard C # tools.

I tried to specify set names when connecting, but MySQL does not understand this encoding.

  • cp1663 is it a typo? - Cyrus
  • I apologize). Fix it) - ice
  • Do not you need in 1251! - Qwertiy
  • one
    Or maybe the conservatory fix? I mean, remake the column in utf8 - tutankhamun

2 answers 2

General view of this:

 var sourceEncoding = Encoding.GetEncoding(1163); var resultEncoding = Encoding.GetEncoding("windows-1251"); byte[] sourceBytes = utf8.GetBytes(text); byte[] resultBytes = Encoding.Convert(sourceEncoding, resultEncoding, sourceBytes); var result = resultEncoding.GetString(resultBytes); 

Only here Code Page I could not find. Here is the specification for CP1163 , you can inherit from System.Text.Encoding and implement Convert , GetBytes .

  • Good option, thanks for the hint. I'll try. - ice
  • Ehm And if encoding a stream of bytes in cp1163 in utf-8 without loss is impossible? - VladD
  • With utf-8 similar, there will be no loss if you implement your Encoding class. - Cyrus
  • 2
    I meant the following situation. You have a stream of bytes that represents a valid string, if you interpret it in cp1163 encoding. You basically propose the following procedure: (1) interpret this stream byte as a line encoded in utf-8, (2) unpack the line again into bytes, (3) turn this stream of bytes into another stream of bytes that corresponds to another encoding, (4 ) convert it to a string. But already at step (1) there may well be a loss of information. And also on the step (3). - VladD
 byte[] smthIn1163 = ...; string s = new string(Encoding.GetEncoding(1163).GetChars(smthIn1163)); 

Well, it is better to create Encoding once, and not in a loop.

  • And why not a getstring rhinestone? - VladD
  • @VladD, I have a similar method with GetString did not work. I had to sculpt the bike as in the first post. - Alexis
  • @ z668: Strange. Why didn't it work exactly ? I've always worked. - VladD
  • 2
    @Qwertiy: from documentation : GetEncoding returns a cached instance with default settings. This is an example of a model with different settings. - VladD
  • 2
    @Qwertiy Just a constructor - protected - ApInvent