gpt4 book ai didi

javascript - 与 JavaScript 中 Python 的 chr() 对应

转载 作者:搜寻专家 更新时间:2023-11-01 00:46:32 36 4
gpt4 key购买 nike

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

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

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

也就是说,我需要一些模仿 JavaScript 的东西

print chr(213) # prints � on the console

最佳答案

原来你只想在 node.js 中使用原始字节,there's a module for that .如果你是一个真正的巫师,你可以让这些东西单独使用 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) # 在控制台上打印 �

所以这会打印一个原始字节 (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 ★

关于javascript - 与 JavaScript 中 Python 的 chr() 对应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11708413/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com