WP8.1 does not coding windows-1251



  • It is necessary to translate the text in the windows 1251 coding into the line. To this end, I use a built-in tool:

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

    However, this code makes an exception:

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

    I can't figure out what's wrong here, on the Internet, and even on an English-speaking stackoverflow, that's the way it is to code and decide.



  • Apparently, System.Text.Encoding supports only three codings:

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

    Accordingly, it cannot find windows-1251. For he doesn't know that code.

    To use the windows 1251, you need to use your class from System.Text.Encoding and describe the code.

    About:

    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 &lt; 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 &lt; 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;
    } 
    

    }




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2