JavaScript 字符转换

发布于 2024-12-04 02:14:18 字数 113 浏览 0 评论 0原文

javascript 函数 charCodeAt() 会将字符转换为 UNICODE 数字。

相反,如果我想查找某个字符的 windows-1252 数字代码,我该怎么做?

谢谢,

The javascript function charCodeAt() will convert a character to the UNICODE numeric.

If, instead, I want to find the windows-1252 numeric code of a character, how can I do that?

Thanks,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

过去的过去 2024-12-11 02:14:18

您是否正在寻找 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 中是相等的。

Are you looking for Windows1251 to 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
];

use for example as String.fromCharCode(winEncToUni[65]) (returns A)

or perhaps Unicode to 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 àèéìòù'));

I have shortened the table quite much: the first half of the codes (0x00-0x7F) are equal in all the codepages (and in Unicode), and the sequence 0xA0-0xFF are equal in Unicode and Windows-1252.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文