System.Text.Encoding.GetEncoding(“iso-8859-1”) thr

2020-02-09 08:26发布

问题:

See subject, note that this question only applies to the .NET compact framework. This happens on the emulators that ship with Windows Mobile 6 Professional SDK as well as on my English HTC Touch Pro (all .NET CF 3.5). iso-8859-1 stands for Western European (ISO), which is probably the most important encoding besides us-ascii (at least when one goes by the number of usenet posts).

I'm having a hard time to understand why this encoding is not supported, while the following ones are supported (again on both the emulators & my HTC):

  • iso-8859-2 (Central European (ISO))
  • iso-8859-3 (Latin 3 (ISO))
  • iso-8859-4 (Baltic (ISO))
  • iso-8859-5 (Cyrillic (ISO))
  • iso-8859-7 (Greek (ISO))

So, is support for say Greek more important than support for German, French and Spanish? Can anyone shed some light on this?

Thanks!

Andreas

回答1:

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)


回答2:

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.



回答3:

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


回答4:

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.



回答5:

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);
    }