It is necessary to translate the text in windows-1251 encoding into a string from the byte array. To do this, I use the built-in tool:

Encoding encoding = Encoding.GetEncoding("windows-1251"); 

However, this code throws an exception:

 'windows-1251' is not a supported encoding name. Parameter name: name 

I just can not understand what is wrong here, on the Internet and even on the English language stackoverflow, this is exactly how the issue with encoding is solved.

    2 answers 2

    Apparently System.Text.Encoding supports only three encodings:

     System.Text.Encoding.BigEndianUnicode System.Text.Encoding.Unicode System.Text.Encoding.UTF8 

    Accordingly, he can not find windows-1251. For such a coding, he does not know.

    To use windows-1251 encoding, you need to implement your class that inherits from System.Text.Encoding and describes this encoding.

    Like that:

     public class Windows1251 : Encoding { static string alpha = "\0\a\b\t\n\v\f\r !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~ЂЃ‚ѓ„…†‡€‰Љ‹ЊЌЋЏђ''“”•–—™љ›њќћџ ЎўЈ¤Ґ¦§Ё©Є«¬®Ї°±Ііґµ¶·ё№є»јЅѕїАБВГДЕЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдежзийклмнопрстуфхцчшщъыьэюя"; public override int GetByteCount(char[] chars, int index, int count) { return count; } public override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex) { byte questionIndex = (byte)alpha.IndexOf('?'); for (int i = 0; i < charCount; i++) { int toIndex = byteIndex + i; int index = alpha.IndexOf(chars[charIndex + i]); if (index == -1) bytes[toIndex] = questionIndex; else bytes[toIndex] = (byte)index; } return charCount; } public override int GetCharCount(byte[] bytes, int index, int count) { return count; } public override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex) { for (int i = 0; i < byteCount; i++) { chars[i + charIndex] = alpha[bytes[byteIndex + i]]; } return byteCount; } public override int GetMaxByteCount(int charCount) { return charCount; } public override int GetMaxCharCount(int byteCount) { return byteCount; } } 
    • The method only accepts a string name , if you pass "1251" - the same thing. The device has the same error as the emulator. About the presence of the encoding did not understand the question. Well, I think it should be, in the list on msdn, it is msdn.microsoft.com/ru-ru/library/… - SUDALV
    • Edited the answer - Alexey
    • then what about windows-1251 encoding? Whether it is supported by Encoding or not, I still need to read it from the byte array. Or is it better to create a separate question for this? - SUDALV
    • Updated the answer ... Immediately, I note, the class code is not mine. I borrowed it somewhere safely when I ran into a similar problem a year ago ... - Alexey
    • So far I just copied myself to the project, wrote the following code: Encoding encoding = new Windows1251(); stringContent = encoding.GetString(bytes, 0, bytes.Length); Encoding encoding = new Windows1251(); stringContent = encoding.GetString(bytes, 0, bytes.Length); error going out of the array, now I will look for where and why - SUDALV

    With the help of the third-party library https://github.com/jstedfast/Portable.Text.Encoding it was easy to decode from the encoding windows-1251. Code example:

     var bytes = await response.Content.ReadAsByteArrayAsync(); Encoding encoding = Portable.Text.Encoding.GetEncoding(1251); var text = encoding.GetString(bytes, 0, bytes.Length);