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

前端 未结 5 1866
我在风中等你
我在风中等你 2020-12-15 22:28

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

相关标签:
5条回答
  • 2020-12-15 23:11

    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);
        }
    
    0 讨论(0)
  • 2020-12-15 23:12

    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;
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-15 23:17

    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.

    0 讨论(0)
  • 2020-12-15 23:22

    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.

    0 讨论(0)
  • 2020-12-15 23:24

    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)
    
    0 讨论(0)
提交回复
热议问题