python - 如果其中一个是字符串,如何对两个十六进制值进行异或?

标签 python python-3.x python-2.7 xor

下面是问题的一个片段

string = "test"
for index, character in enumerate(string):
     value = hex(ord(character)) ^ 0xababab

此代码返回一个错误,它不能对字符串和整数进行 XOR。那么我怎样才能让 python 将 hex(ord(character)) 作为整数而不是字符串来处理呢?

我尝试通过添加 int(hex(ord(character))) 将其转换为 int,但返回错误:Invalid literal for int with base10

最佳答案

就用ord吧,它是int。

for c in  s:
    print(ord(c) ^ 0xababab)

如您所见,它们是等价的:

1 == 0x01 # True

关于python - 如果其中一个是字符串,如何对两个十六进制值进行异或?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55430071/

相关文章:

python - 像 Python 这样简单易学的语言,但需要声明变量?

python - 使用 Django 时如何在命令行中退出 dbshel​​l (SQLite 3)?

python - 除了面部编码数组之外的其他数据存储方式

python-2.7 - 我的opencv python程序显示看似期望的短语,但既不终止也不允许进度

python - 在两个独立的Python程序之间传输数据有哪些好方法?

python - Cookiecutter Django 与 PostgreSQL 不兼容

Python-Dymola 接口(interface) : How is it possible to check in real time the progress of a Dymola model?

python - pandas 将标题与行进行比较并删除重复的行

python - PyCharm & Pyenv 本地?