php - 在Python中使用二元运算符...翻译 if (c1 >= "\xc0"& c1 <= "\xdf")

标签 php python

我正在转换外部 class从 PHP 到 Python,它做了一些技巧,例如:

if ($c1 >= "\xc0" & $c1 <= "\xdf")
[...]
$cc1 = (chr(ord($c1) / 64) | "\xc0");
[...]
$cc2 = ($c1 & "\x3f") | "\x80";

其中 $c1,^$cc1, $cc2 是字符

我刚刚意识到我不能在Python中使用它,因为字符是字符串,并且不会重复地被视为“字符的二进制表示”,其中运算符&和|有道理...

请问您如何以Pythonic 方式翻译其中任何一个?

>>> c1 = "a"
>>> (c1 & "\x3f") | "\x80"

Traceback (most recent call last):
  File "<pyshell#202>", line 1, in <module>
    (c1 & "\x3f") | "\x80"
TypeError: unsupported operand type(s) for &: 'str' and 'str'

编辑:实际上,这个 PHP 类似乎不起作用,所以它也不能满足我的需求。非常感谢您的帮助。

最佳答案

使用ord函数获取值,然后使用实际数字进行屏蔽。

>>> c1 = "a"
>>> (ord(c1) & 0x3f) | 0x80
161
>>> hex((ord(c1) & 0x3f) | 0x80)
'0xa1'

关于php - 在Python中使用二元运算符...翻译 if (c1 >= "\xc0"& c1 <= "\xdf"),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14776950/

相关文章:

python - 如何解决 Python 3.6 中的此导入错误?

python - 在不循环的情况下转换数据框?

python - 在 Dropbox API Python 上实现 OAuth

python 字符串列表转换为字典列表

php - 非常简单的 PHP 代码使 Apache 崩溃

php - Ajax、php 输出、iframe 和 JCrop

php - 使用 nginx 的 wordpress 空白页面

python - 如何从 Django Rest Framework 序列化程序返回不同时区的 DateTime

php - 未收到来自 Amazon SNS 的 GCM 推送通知

php - 调用不存在的方法时重定向到其他方法