简体   繁体   English

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

[英]using binary operator in Python… translating if (c1 >= “\xc0” & c1 <= “\xdf”)

i am converting an external class from PHP to Python, it does some tricks like : 我正在将一个外部从PHP转换为Python,它做了一些技巧,如:

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

where $c1,^$cc1, $cc2 are characters 其中$ c1,^ $ cc1,$ cc2是字符

and i just realized that i cannot use it as such with python, as characters are string, and not duplicately seen as "binary representation of a character" where operators & and | 我只是意识到我不能像python一样使用它,因为字符是字符串,而不是重复地看作“字符的二进制表示”,其中运算符&和| make sense... 说得通...

please, how would you translate any of these in a Pythonic way ? 拜托,你怎么用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'

EDIT: actually, it seems that this PHP class do not work, so it will not fit my needs either. 编辑:实际上,似乎这个PHP类不起作用,所以它也不适合我的需要。 Many thanks for your help. 非常感谢您的帮助。

That's a primitive UTF-8 encoding function. 这是一个原始的UTF-8编码功能。

c1.encode('utf-8')

Note that unless you use unicode s natively (and why aren't you?) you'll need to decode from 'latin-1' first. 请注意,除非你本地使用unicode (为什么不是你?),你需要先从'latin-1'解码。

Use the ord function to get the value and then use actual numbers to do the masking. 使用ord函数获取值,然后使用实际数字进行屏蔽。

>>> c1 = "a"
>>> (ord(c1) & 0x3f) | 0x80
161
>>> hex((ord(c1) & 0x3f) | 0x80)
'0xa1'
Python 2.7.3 (default, Sep 26 2012, 21:51:14) 
>>> c1 = 'd'
>>> # if ($c1 >= "\xc0" & $c1 <= "\xdf")
... 
>>> ord(c1) >= 0xc0 and ord(c1) <= 0xdf
False
>>> # $cc1 = (chr(ord($c1) / 64) | "\xc0");
... 
>>> chr(ord(c1) / 64 | 0xc0)
'\xc1'
>>> # $cc2 = ($c1 & "\x3f") | "\x80";
... 
>>> ord(c1) & 0x3f | 0x80
164
>>> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM