具有非ASCII字符的RPython ord() [英] RPython ord() with non-ascii character

查看:126
本文介绍了具有非ASCII字符的RPython ord()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PyPy在RPython中创建虚拟机.我的问题是,我正在将每个字符转换为数字表示形式.例如,将字母"a"转换为结果97,然后将97转换为十六进制,因此得到:0x61.

I'm making a virtual machine in RPython using PyPy. My problem is, that I am converting each character into the numerical representation. For example, converting the letter "a" provides this result, 97. And then I convert the 97 to hex, so I get: 0x61.

例如,我尝试将字母á"转换为十六进制表示形式,应为:0xe1,但我却得到0xc3 0xa1

So for example, I'm trying to convert the letter "á" into the hexadecimal representation which should be: 0xe1 but instead I get 0xc3 0xa1

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

Is there a specific encoding I need to use? Currently I'm using UTF-8.

-更新-

instr是"á"的地方(包括引号)

Where instr is "á", (including the quotes)

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

推荐答案

#!/usr/bin/env python
# -*- coding: latin-1 -*-

char = 'á'

print str(int(ord(char)))
print hex(int(char))
print char.decode('latin-1')

给我:

225
0xe1
0xe1

这篇关于具有非ASCII字符的RPython ord()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆