gpt4 book ai didi

python - 如何在 Python3 中实现 Javascript String.fromCharCode 以接受减号

转载 作者:太空宇宙 更新时间:2023-11-04 09:22:56 25 4
gpt4 key购买 nike

我必须像下面的 Javascript 代码一样在 Python 中创建一个角色:

char = String.fromCharCode(-31)
console.log(char);

这是我的代码:

chr(-31)

但我在 python 中遇到错误:ValueError: chr() arg not in range(0x110000)

我知道问题是将负数传递给 chr 函数,但我不知道女巫函数能够处理负数。

最佳答案

我认为 Python 的行为在这里更好:您传递的内容没有定义的 Unicode 代码点!也就是说,the JavaScript docs说:

A sequence of numbers that are UTF-16 code units. The range is between 0 and 65535 (0xFFFF). Numbers greater than 0xFFFF are truncated. No validity checks are performed.

所以在 Python 中你只需要做:

chr(-31 & 0xffff)

关于python - 如何在 Python3 中实现 Javascript String.fromCharCode 以接受减号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59174876/

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