System.Text.Encoding.GetEncoding(“iso-8859-1”) throws PlatformNotSupportedException?

人走茶凉 提交于 2019-11-29 01:33:58

I would try to use "windows-1252" as encoding string. According to Wikipedia, Windows-1252 is a superset of ISO-8859-1.

System.Text.Encoding.GetEncoding(1252)

This MSDN article says:

The .NET Compact Framework supports character encoding on all devices: Unicode (BE and LE), UTF8, UTF7, and ASCII.

There is limited support for code page encoding and only if the encoding is recognized by the operating system of the device.

The .NET Compact Framework throws a PlatformNotSupportedException if the a required encoding is not available on the device.

I believe all (or at least many) of the ISO encodings are code-page encodings and fall under the "limited support" rule. UTF8 is probably your best bet as a replacement.

hdkrus

I know its a bit later but I made an implementation for .net cf of encoding ISO-8859-1, I hope this could help:

namespace System.Text
{
    public class Latin1Encoding : Encoding
    {
        private readonly string m_specialCharset = (char) 0xA0 + @"¡¢£¤¥¦§¨©ª«¬­®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖרÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ";

        public override string WebName
        {
            get { return @"ISO-8859-1"; }
        }

        public override int CodePage
        {
            get { return 28591; }
        }

        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)
        {
            if (chars == null)
                throw new ArgumentNullException(@"chars", @"null array");
            if (bytes == null)
                throw new ArgumentNullException(@"bytes", @"null array");
            if (charIndex < 0)
                throw new ArgumentOutOfRangeException(@"charIndex");
            if (charCount < 0)
                throw new ArgumentOutOfRangeException(@"charCount");
            if (chars.Length - charIndex < charCount)
                throw new ArgumentOutOfRangeException(@"chars");
            if (byteIndex < 0 || byteIndex > bytes.Length)
                throw new ArgumentOutOfRangeException(@"byteIndex");

            for (int i = 0; i < charCount; i++)
            {
                char ch = chars[charIndex + i];
                int chVal = ch;
                bytes[byteIndex + i] = chVal < 160 ? (byte)ch : (chVal <= byte.MaxValue ? (byte)m_specialCharset[chVal - 160] : (byte)63);
            }

            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)
        {
            if (chars == null)
                throw new ArgumentNullException(@"chars", @"null array");
            if (bytes == null)
                throw new ArgumentNullException(@"bytes", @"null array");
            if (byteIndex < 0)
                throw new ArgumentOutOfRangeException(@"byteIndex");
            if (byteCount < 0)
                throw new ArgumentOutOfRangeException(@"byteCount");
            if (bytes.Length - byteIndex < byteCount)
                throw new ArgumentOutOfRangeException(@"bytes");
            if (charIndex < 0 || charIndex > chars.Length)
                throw new ArgumentOutOfRangeException(@"charIndex");

            for (int i = 0; i < byteCount; ++i)
            {
                byte b = bytes[byteIndex + i];
                chars[charIndex + i] = b < 160 ? (char)b : m_specialCharset[b - 160];
            }

            return byteCount;
        }

        public override int GetMaxByteCount(int charCount)
        {
            return charCount;
        }

        public override int GetMaxCharCount(int byteCount)
        {
            return byteCount;
        }
    }
}

It is odd that 8859-1 isn't supported, but that said, UTF-8 does have the ability to represent all of teh 8859-1 characters (and more), so is there a reason you can't just use UTF-8 instead? That's what we do internally, and I just dealt with almost this same issue today. The plus side of using UTF-8 is that you get support for far-east and cyrillic languages without making modifications and without adding weight to the western languages.

If anyone is getting an exception(.NET compact framework) like:

 System.Text.Encoding.GetEncoding(“iso-8859-1”) throws PlatformNotSupportedException,

//Please follow the steps: 

   byte[] bytes=Encoding.Default.GetBytes(yourText.ToString);

//The code is:

    FileInfo fileInfo = new FileInfo(FullfileName); //file type :  *.text,*.xml 
    string yourText = (char) 0xA0 + @"¡¢£¤¥¦§¨©ª«¬­®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖרÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ";

    using (FileStream s = fileInfo.OpenWrite()) {
                        s.Write(Encoding.Default.GetBytes(yourText.ToString()), 0, yourText.Length);
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!