2008-12-29 29 views
13

請參閱主題,請注意,此問題僅適用於.NET compact框架。這發生在隨Windows Mobile 6 Professional SDK以及我的英文HTC Touch Pro(所有.NET CF 3.5)一起提供的模擬器上。 iso-8859-1代表西歐(ISO),這可能是除我們之外最重要的編碼 - ascii(至少當用戶網的帖子數量減去時)。System.Text.Encoding.GetEncoding(「iso-8859-1」)引發PlatformNotSupportedException?

我有一個很難理解爲什麼不支持這種編碼,而以下的有(再次在兩個模擬器&我的HTC)的支持:

  • ISO-8859-2(中央歐洲(ISO))
  • ISO-8859-3(拉丁語3(ISO))
  • ISO-8859-4(波羅的海(ISO))
  • ISO-8859-5(西里爾語(ISO))
  • iso-8859-7(希臘語(ISO))

那麼,支持說希臘語比支持德語,法語 和西班牙語更重要嗎?任何人都可以對此有所瞭解嗎?

謝謝!

安德烈亞斯

回答

6

This MSDN article說:

的.NET Compact Framework的支持上的所有設備 字符編碼: 的Unicode(BE和LE),UTF8,UTF7,和ASCII 。

對代碼頁 編碼的支持有限,只有編碼爲 的設備才能被操作系統 識別。

如果設備上沒有所需的編碼,則.NET Compact Framework將拋出 PlatformNotSupportedException。

我相信所有(或至少很多)ISO編碼都是代碼頁編碼,屬於「有限支持」規則。 UTF8可能是您作爲替代品的最佳選擇。

+0

嗨 似乎沒有任何鏈接的網頁包含能回答這個問題的信息「爲什麼是ISO-8859-7支持,而ISO-8859-1是不是? 「 – 2008-12-29 21:24:15

+0

我認爲「對代碼頁支持的有限支持」相當簡潔地包裝起來了,我自己。我相信你堅持使用UTF8。 – Otis 2008-12-29 21:36:53

15

我會嘗試使用「windows-1252」作爲編碼字符串。 According to Wikipedia,Windows-1252是ISO-8859-1的超集。

System.Text.Encoding.GetEncoding(1252) 
+2

嗨 這種方法在某些情況下非常麻煩,例如,當你有一個ISO-8859-1編碼的XML流時。在將其提供給XML閱讀器之前,您需要將「iso-8859-1」編碼替換爲「windows-1252」。 – 2008-12-29 21:30:35

0

您是否嘗試過大寫字符集名稱? official註冊不包含您提供的小寫字母名稱(不解釋爲什麼它接受其他ISO-8859變體的小寫版本)。

Name: ISO_8859-1:1987         [RFC1345,KXS2] 
MIBenum: 4 
Source: ECMA registry 
Alias: iso-ir-100 
Alias: ISO_8859-1 
Alias: ISO-8859-1 (preferred MIME name) 
Alias: latin1 
Alias: l1 
Alias: IBM819 
Alias: CP819 
Alias: csISOLatin1 
0

奇怪的是,8859不支持,而是說,UTF-8的確有代表所有TEH 8859-1字符(及以上)的能力,所以是有一個原因,你不能僅僅使用UTF-8呢?這就是我們內部做的事情,而我今天剛剛處理了幾乎同樣的問題。使用UTF-8的好處在於,您可以獲得對遠東語和西里爾語的支持,無需進行修改,也不會增加西語的重量。

3

我知道它有點晚,但我做了編碼ISO-8859-1的.NET CF的實現,我希望這能幫助:

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

如果有人得到一個異常(.NET緊湊框架),如:

System.Text.Encoding.GetEncoding(「iso-8859-1」) throws PlatformNotSupportedException, 

請遵循的步驟:

字節[]字節= Encoding.Default.GetBytes(yourText.ToString);

的代碼是:

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); 
} 
相關問題