简体   繁体   English

Python字符串到字节的转换

[英]Python String to byte conversion

x = '192.168.1.1'
y = '255.255.255.0'
a = x.split('.')
b = y.split('.')
a[0] & b[0]

Traceback (most recent call last): 追溯(最近一次通话):

File "<pyshell#35>", line 1, in <module>
a[0] & b[0]
TypeError: unsupported operand type(s) for &: 'str' and 'str'

Please help, I'd like to have the result of and-ing process between 192 and 255, between 168 and 255. How can I do that? 请帮忙,我希望192和255之间以及168和255之间的结果与运算结果。我该怎么做?

you need to convert the strings to integers and & those together 您需要将字符串转换为整数,然后将&

however on of the properties of 255 is that it is eight 1 s so anding it together with a smaller number is always equal to the other number 但是255的属性是8个1 s,因此将其与较小的数字相加总是等于另一个数字

You need to first convert the strings into integers. 您需要首先将字符串转换为整数。 Try this: 尝试这个:

x = '192.168.1.1'
y = '255.255.255.0'
a = [int(elem) for elem in x.split('.')]
b = [int(elem) for elem in y.split('.')]
a[0] & b[0]

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

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