【问题标题】:Python, cast Integer to bytes.Python,将整数转换为字节。
【发布时间】:2013-04-15 05:24:26
【问题描述】:

我是 python 新手,不能做一些简单的事情。 我有一个来自 java 的代码

raw[j] = (byte) (chksum & 0xff)
raw[j + 1] = (byte) (chksum >> 0x08 & 0xff)
raw[j + 2] = (byte) (chksum >> 0x10 & 0xff)
raw[j + 3] = (byte) (chksum >> 0x18 & 0xff)

其中 raw 是一个字节数组 我需要用python来做。但是我怎样才能将 int 转换为字节? 也许有人知道我在哪里可以获得使用 Blowfish 加密的库?

【问题讨论】:

    标签: python-3.x bytearray blowfish


    【解决方案1】:

    使用struct 模块来转换字节。

    你的例子是:

    raw[j] = struct.pack("B", chksum & 0xff)
    etc..
    

    参考:http://docs.python.org/3/library/struct.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-27
      • 1970-01-01
      • 2012-09-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多