发布时间:2019-08-12 11:50:48编辑:auto阅读(2805)
字符串之间是无法直接进行加法运算的,要先经过转换。
十六进制字符串转换为十进制
int('a',16)
int('0xa',16)
十进制转换为十六进制
hex(10)
'0xa'
十进制转换为字符串
str(12)
'12'
练习:求MAC地址的下一个地址,考虑01 0f结尾的情况。
#!/usr/bin/python
macaddr = '00:16:3E:00:69:0D'
prefix = macaddr[:-2]
last_two = macaddr[-2:]
last_two_int = int(last_two,16)
new_last_two_int = last_two_int + 1
new_last_two = hex(new_last_two_int)
if len(new_last_two) == 3:
new_last_two = '0' + new_last_two[-1:]
else:
new_last_two = new_last_two[-2:]
newmacaddr = prefix + new_last_two
print newmacaddr.upper()
上一篇: selenium RC 与python集
下一篇: Python PK shell ----
53526
40351
34753
30511
25389
25183
23594
18868
15174
14681
1150°
1072°
1143°
1159°
1196°
1351°
1311°
1286°
1381°
1326°