windows - Javascript tegn konvertering

Indlæg af Hanne Mølgaard Plasc

Problem



Javascript-funktionen charCodeAt () konverterer et tegn til UNICODE-numerisk.


Hvis jeg i stedet vil finde Windows-1252-numerisk kode for et tegn, hvordan kan jeg gøre det?


Tak,

Bedste reference


Leder du efter Windows1251 til 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
];


brug for eksempel som String.fromCharCode(winEncToUni[65]) (returnerer A)


eller måske Unicode til 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 àèéìòù'));


Jeg har forkortet bordet ret meget: Den første halvdel af koderne (0x00-0x7F) er ens i alle kodesiderne (og i Unicode), og sekvensen 0xA0-0xFF er ens i Unicode og Windows-1252.