【Python】TypeError: a bytes-like object is required, not 'str'

1、报错

自己python3.+,使用别人python2.的代码报错:

rs = pow(int(binascii.hexlify(text), 16), int(pubKey, 16), int(modulus, 16))
TypeError: a bytes-like object is required, not 'str'

2、原因

因为python3是bytes-like的,只能把它转为str

3、解决

  # bytes object
  b = b"example"

  # str object
  s = "example"

  # str to bytes
  s=bytes(s, encoding = "utf8")

  # bytes to str
  b=str(b, encoding = "utf-8")

  # an alternative method
  # str to bytes
  str.encode(s)

  # bytes to str
  bytes.decode(b)

4、参考

https://blog.csdn.net/csu_vc/article/details/78372932

你可能感兴趣的:(python)