2011-09-12 35 views
2

javascript函數charCodeAt()會將字符轉換爲UNICODE數字。Javascript字符轉換

如果我想找到一個字符的windows-1252數字代碼,我該怎麼做?

感謝,

+1

我想你必須創建自己的映射表。 JavaScript本身並不知道有關該字符編碼的任何內容。 – Pointy

+0

我認爲,對於代碼頁Win-1252,你可以使用'var char = String.fromCharCode(nonUnicode_Win1252code)'作爲字母數字字符,然後將結果的unicode'char'與目標進行比較。但對於所有其他代碼頁我不知道該怎麼做。例如_String.fromCharCode(65,66,67)===「ABC」_ –

回答

3

您是否在尋找Windows1251到Unicode:

var winEncToUni = [ 
    0, 1, 2, 3, 4, 5, 6, 7, 
    8, 9, 10, 11, 12, 13, 14, 15, 
    16, 17, 18, 19, 20, 21, 22, 23, 
    24, 25, 26, 27, 28, 29, 30, 31, 
    32, 33, 34, 35, 36, 37, 38, 39, 
    40, 41, 42, 43, 44, 45, 46, 47, 
    48, 49, 50, 51, 52, 53, 54, 55, 
    56, 57, 58, 59, 60, 61, 62, 63, 
    64, 65, 66, 67, 68, 69, 70, 71, 
    72, 73, 74, 75, 76, 77, 78, 79, 
    80, 81, 82, 83, 84, 85, 86, 87, 
    88, 89, 90, 91, 92, 93, 94, 95, 
    96, 97, 98, 99, 100, 101, 102, 103, 
    104, 105, 106, 107, 108, 109, 110, 111, 
    112, 113, 114, 115, 116, 117, 118, 119, 
    120, 121, 122, 123, 124, 125, 126, 127, 
    8364, 129, 8218, 402, 8222, 8230, 8224, 8225, 
    710, 8240, 352, 8249, 338, 141, 381, 143, 
    144, 8216, 8217, 8220, 8221, 8226, 8211, 8212, 
    732, 8482, 353, 8250, 339, 157, 382, 376, 
    160, 161, 162, 163, 164, 165, 166, 167, 
    168, 169, 170, 171, 172, 173, 174, 175, 
    176, 177, 178, 179, 180, 181, 182, 183, 
    184, 185, 186, 187, 188, 189, 190, 191, 
    192, 193, 194, 195, 196, 197, 198, 199, 
    200, 201, 202, 203, 204, 205, 206, 207, 
    208, 209, 210, 211, 212, 213, 214, 215, 
    216, 217, 218, 219, 220, 221, 222, 223, 
    224, 225, 226, 227, 228, 229, 230, 231, 
    232, 233, 234, 235, 236, 237, 238, 239, 
    240, 241, 242, 243, 244, 245, 246, 247, 
    248, 249, 250, 251, 252, 253, 254, 255 
]; 

使用例如作爲String.fromCharCode(winEncToUni[65])(返回A

或者是Unicode向Windows1251?

function toWindows1252(string, replacement) 
{ 
    var ret = new Array(string.length);  
    var i, ch; 

    replacement = typeof replacement === "string" && replacement.length > 0 ? replacement.charCodeAt(0) : 0; 

    for (i = 0; i < string.length; i++) 
    { 
     ch = string.charCodeAt(i); 

     if (ch <= 0x7F || (ch >= 0xA0 && ch <= 0xFF)) 
     { 
      ret[i] = ch; 
     } 
     else 
     { 
      ret[i] = toWindows1252.table[string[i]]; 

      if (typeof ret[i] === "undefined") 
      { 
       ret[i] = replacement; 
      } 
     } 
    } 

    return ret; 
} 

toWindows1252.table = { 
    '\x81': 129, '\x8d': 141, '\x8f': 143, '\x90': 144, 
    '\x9d': 157, '\u0152': 140, '\u0153': 156, '\u0160': 138, 
    '\u0161': 154, '\u0178': 159, '\u017d': 142, '\u017e': 158, 
    '\u0192': 131, '\u02c6': 136, '\u02dc': 152, '\u2013': 150, 
    '\u2014': 151, '\u2018': 145, '\u2019': 146, '\u201a': 130, 
    '\u201c': 147, '\u201d': 148, '\u201e': 132, '\u2020': 134, 
    '\u2021': 135, '\u2022': 149, '\u2026': 133, '\u2030': 137, 
    '\u2039': 139, '\u203a': 155, '\u20ac': 128, '\u2122': 153 
}; 

document.write(toWindows1252('Hello World àèéìòù')); 

我縮短了表頗多:代碼(爲0x00-0x7F)上半年在所有代碼頁相等(和Unicode)的,序列0xA0-0xFF爲Unicode和Windows等-1252。

+0

我認爲他實際上需要Unicode到Windows,這意味着一個更大的表格:-) – Pointy

+1

@Pointy:技術上不是:-)仍是目的地是256長! – xanatos

+0

非常好的答案,謝謝! –