Python2.7
首先是服务器穿过来的字节流,需要转换为 string(按照 ascii),比如 16 进制字节流是
"31 35 37 50 55 00 00 00 00 00"
需要转为 string
"157PU"
于是有下列代码
def byte_to_string(d):
    return binascii.a2b_hex(d)
但实际转换结果为
a = byte_to_string('31 35 37 50 55 00 00 00 00 00')
print a
>>>"157PU\x00\x00\x00\x00\x00"
请问这个 string 里几个\x00 怎么删除,我用
a.strip()
a.replace("\n","")
都不好使,最用用 len(a)测试的时候结果都还是 10
|  |      1shuax      2021-03-29 16:03:29 +08:00 s = "31 35 37 50 55 00 00 00 00 00" b = bytes.fromhex(s) s = b.decode() s = s.replace('\0', '') | 
|  |      2ruanimal      2021-03-29 16:18:36 +08:00 "157PU\x00\x00\x00\x00\x00".strip('\x00') | 
|      3Keyes      2021-03-29 16:35:42 +08:00 via iPhone 这种直接把一整个缓冲区丢出来的服务端真心蛋疼 | 
|      4Olament      2021-03-29 16:58:13 +08:00 a.rstrip('\x00') | 
|      5julyclyde      2021-03-29 17:08:47 +08:00 用\n 那不是显然不好使么…… | 
|  |      6fuis      2021-03-29 20:06:30 +08:00 用楼主的话来回复楼主: 没有十年脑。。。。 算了 | 
|  |      7dingwen07      2021-03-29 22:49:11 +08:00 via iPhone a.strip('\x00')不行就 a.strip('\\x00') | 
|  |      8iptables      2021-03-30 05:06:23 +08:00 >>> "157PU\x00\x00\x00\x00\x00" '157PU\x00\x00\x00\x00\x00' >>> a = "157PU\x00\x00\x00\x00\x00" >>> a.rstrip('\x00') '157PU' >>> a.rstrip('\n') '157PU\x00\x00\x00\x00\x00' >>> a.rstrip('\0') '157PU' | 
|  |      9iptables      2021-03-30 05:07:46 +08:00 \n 相当于 \x0a,和 \x00 完全不一样啊 |