Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
带有非ascii字符的RPython ord()_Python_Unicode_Utf 8_Pypy_Rpython - Fatal编程技术网

带有非ascii字符的RPython ord()

带有非ascii字符的RPython ord(),python,unicode,utf-8,pypy,rpython,Python,Unicode,Utf 8,Pypy,Rpython,我正在用PyPy在RPython中创建一个虚拟机。我的问题是,我正在将每个字符转换为数字表示。例如,转换字母a将提供此结果97。然后我把97转换成十六进制,得到0x61 例如,我试图将字母a转换成十六进制表示形式,应该是:0xe1,但是我得到的是0xc3 0xa1 我需要使用特定的编码吗?目前我正在使用UTF-8 -更新- instr在哪里,包括引号 for char in instr: char = str(int(ord(char))) char = hex(int(char

我正在用PyPy在RPython中创建一个虚拟机。我的问题是,我正在将每个字符转换为数字表示。例如,转换字母a将提供此结果97。然后我把97转换成十六进制,得到0x61

例如,我试图将字母a转换成十六进制表示形式,应该是:0xe1,但是我得到的是0xc3 0xa1

我需要使用特定的编码吗?目前我正在使用UTF-8

-更新-

instr在哪里,包括引号

for char in instr:
    char = str(int(ord(char)))
    char = hex(int(char))
    char = char[2:]
    print char # Prints 22 C3 A1 22, 22 is each of the quotes
    # The desired output is 22 E1 22
给我:

225
0xe1
0xe1

您使用的是Python语言的版本2,因此您的string:á是一个字节字符串,其内容取决于源文件的编码。如果编码为UTF-8,则为C3 A1-字符串包含两个字节

如果要将其转换为Unicode码点(也称字符)或UTF-16码点(取决于Python安装),请首先将其转换为Unicode,例如使用.decode'UTF-8'

产出:

c3
a1
-------
e1

这还没有解决它,我仍然得到C3 A1而不是E1。谢谢你的帮助!您可以发布用于打印十六进制值的代码片段吗?我得到0xe1。我在问题中添加了代码。再次感谢你的帮助@用户3566150我更新了我的答案。我在某个地方读到,这可能与处理器有关,那么这是我的电脑吗?我用的是Mac电脑。当我运行代码时,它显示TypeError:ord需要一个字符,但找到了长度为2的字符串
# -*- encoding: utf-8 -*-

def stuff(instr):
  for char in instr:
    char = str(int(ord(char)))
    char = hex(int(char))
    # I'd replace those two lines above with char = hex(ord(char))
    char = char[2:]
    print char 

stuff("á")
print("-------")
stuff(u"á")
c3
a1
-------
e1