2011-05-30 139 views

回答

2

這對UCS2是正確的,但這很可能不是你所擁有的。現在,你更可能遇到UTF-16。與UCS-2不同,UTF-16將Unicode字符編碼爲一個或兩個16位單元。這是必要的,因爲Unicode在當前版本中的字符數超過了65536個。

更復雜的轉換通常可以由您的操作系統完成,並且有幾個(非標準)庫提供相同的功能,例如, ICU。

+0

謝謝,但ICU對我來說太大了。 – PDF1001 2011-05-31 02:04:14

0

我有類似的東西。希望它會幫助:

String^ StringFromUCS4(const char32_t* element, int length) 
{ 
    StringBuilder^ result = gcnew StringBuilder(length); 
    const char32_t* pUCS4 = element; 

    int characterCount = 0; 
    while (*pUCS4 != 0) 
    { 
     wchar_t cUTF16; 
     if (*pUCS4 < 0x10000) 
     { 
      cUTF16 = (wchar_t)*pUCS4; 
     } 
     else 
     { 
      unsigned int t = *pUCS4 - 0x10000; 
      unsigned int h = (((t << 12) >> 22) + 0xD800); 
      unsigned int l = (((t << 22) >> 22) + 0xDC00); 
      cUTF16 = (wchar_t)((h << 16) | (l & 0x0000FFFF)); 
     } 

     result->Append((wchar_t)*pUCS4); 

     characterCount++; 
     if (characterCount >= length) 
     { 
      break; 
     } 

     pUCS4++; 
    } 

    return result->ToString(); 
} 
相關問題