1

関数fromCharCodeは、国際的な ANSI 文字では機能しません。たとえば、ID が 192 ~ 223 のロシア語 ANSI (cp-1251) 文字の場合、特殊文字が返されます。この問題を解決するにはどうすればよいですか?

ANSI char ID を Unicode char ID に変換してから使用する必要があると思いますfromCharCode。しかし、ANSI char ID を Unicode char ID に変換するにはどうすればよいでしょうか (現在のロケール/コードページによって異なります)。

助けてくれてありがとう!

4

2 に答える 2

3

Considering that you know code page that your data is encoded in, just set up a mapping Object with keys being codes in your code page and values being proper Unicode symbols or numeric codepoints and use it to convert your data.

mapFromCP1251 = {
   192: 'А',
   193: 'Б',
   194: 'В',
   197: 'Е',
   200: 'И',
   204: 'М',
   207: 'П',
   208: 'Р',
   210: 'Т'
   // etc, I don't feel like typing entire http://en.wikipedia.org/wiki/CP1251 here
}

var string = mapFromCP1251[192] + mapFromCP1251[192] + mapFromCP1251[192] + mapFromCP1251[193] + mapFromCP1251[193] + mapFromCP1251[194]
alert(string) // АААББВ
alert(mapFromCP1251[207]+mapFromCP1251[208]+mapFromCP1251[200]+mapFromCP1251[194]+mapFromCP1251[197]+mapFromCP1251[210]+", "+mapFromCP1251[204]+mapFromCP1251[200]+mapFromCP1251[208]+"!") // Hello, world!
于 2012-08-09T12:58:24.903 に答える