3

JavaScript 方法在以下意义上String.fromCharCode()与 Python 的行为等效:unichar()

print unichr(213) # prints Õ on the console 
console.log(String.fromCharCode(213)); // prints Õ on the console as well

但是,出于我的目的,我需要一个与 Python 函数等效的 JavaScript chr()。是否有这样的 JavaScript 函数或使String.fromCharCode()行为类似的方法chr()

也就是说,我需要在 JavaScript 中模仿的东西

print chr(213) # prints � on the console
4

2 回答 2

3

所以结果你只想在 node.js 中使用原始字节,有一个模块可以做到。如果你是一个真正的巫师,你可以让这些东西单独使用 javascript 字符串,但它更难,效率也低得多。

var b = new Buffer(1);
b[0] = 213;

console.log(b.toString()); //�


var b = new Buffer(3);
b[0] = 0xE2;
b[1] = 0x98;
b[2] = 0x85;

console.log(b.toString()); //★

print chr(213) # prints � on the console

因此,这会打印一个原始字节 ( 0xD5),它以 UTF-8(很可能)解释,这不是有效的 UTF-8 字节序列,因此显示为替换字符 (�)。

UTF-8 的解释与此处无关,您很可能只需要原始字节。

要在 javascript 中创建原始字节,您可以使用UInt8Array.

var a = new Uint8Array(1);
a[0] = 213;

您可以选择将原始字节解释为 utf-8:

console.log( utf8decode(a)); // "�"

//Not recommended for production use ;D
//Doesn't handle > BMP to keep the answer shorter
function utf8decode(uint8array) {
    var codePoints = [],
        i = 0,
        byte, codePoint, len = uint8array.length;
    for (i = 0; i < len; ++i) {
        byte = uint8array[i];

        if ((byte & 0xF8) === 0xF0 && len > i + 3) {

            codePoint = ((byte & 0x7) << 18) | ((uint8array[++i] & 0x3F) << 12) | ((uint8array[++i] & 0x3F) << 6) | (uint8array[++i] & 0x3F);
            if (!(0xFFFF < codePoint && codePoint <= 0x10FFFF)) {
                codePoints.push(0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD);
            } else {
                codePoints.push(codePoint);
            }
        } else if ((byte & 0xF0) === 0xE0 && len > i + 2) {

            codePoint = ((byte & 0xF) << 12) | ((uint8array[++i] & 0x3F) << 6) | (uint8array[++i] & 0x3F);
            if (!(0x7FF < codePoint && codePoint <= 0xFFFF)) {
                codePoints.push(0xFFFD, 0xFFFD, 0xFFFD);
            } else {
                codePoints.push(codePoint);
            }
        } else if ((byte & 0xE0) === 0xC0  && len > i + 1) {

            codePoint = ((byte & 0x1F) << 6) | ((uint8array[++i] & 0x3F));
            if (!(0x7F < codePoint && codePoint <= 0x7FF)) {
                codePoints.push(0xFFFD, 0xFFFD);
            } else {
                codePoints.push(codePoint);
            }
        } else if ((byte & 0x80) === 0x00) {
            codePoints.push(byte & 0x7F);
        } else {
            codePoints.push(0xFFFD);
        }
    }
    return String.fromCharCode.apply(String, codePoints);
}

您最有可能尝试做的事情与尝试将字节解释为 utf8 无关。

另一个例子:

//UTF-8 For the black star U+2605 ★:
var a = new Uint8Array(3);
a[0] = 0xE2;
a[1] = 0x98;
a[2] = 0x85;
utf8decode(a) === String.fromCharCode(0x2605) //True
utf8decode(a) // ★

在 python 2.7 (Ubuntu) 中:

print chr(0xE2) + chr(0x98) + chr(0x85)
#prints ★
于 2012-07-29T11:01:45.040 回答
1

如果你想为每个不在标准 ASCII 表中的数字设置这个“盒子里的问号”,这个小函数怎么样?

function chr(c) {
    return (c < 0 || c > 126) ? '�' : String.fromCharCode(c);
}
于 2012-07-29T10:49:29.710 回答