相关疑难解决方法(0)

在Python 3中将字符串转换为字节的最佳方法?

似乎有两种不同的方法将字符串转换为字节,如TypeError的答案所示:'str'不支持缓冲区接口

哪种方法更好或更好Pythonic?或者只是个人喜好?

b = bytes(mystring, 'utf-8')

b = mystring.encode('utf-8')
Run Code Online (Sandbox Code Playgroud)

python string character-encoding python-3.x

734
推荐指数
5
解决办法
110万
查看次数

将可变大小的字节数组转换为整数/长整数

如何将(大端)可变大小的二进制字节数组转换为(无符号)整数/长?例如,'\x11\x34'代表4404

现在,我正在使用

def bytes_to_int(bytes):
  return int(bytes.encode('hex'), 16)
Run Code Online (Sandbox Code Playgroud)

哪个小而且有点可读,但可能效率不高.有更好的(更明显的)方式吗?

python int bytearray

35
推荐指数
1
解决办法
7万
查看次数

标签 统计

python ×2

bytearray ×1

character-encoding ×1

int ×1

python-3.x ×1

string ×1