Search code examples
python-3.xarraysblowfish

Python, cast Integer to bytes.


i am newbie in python, and cannot do some easy stuf. I have a code from 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)

Where raw is a array of bytes I need to do it in python. But how can i cast int to bytes? And maybe anybody knows where can i get the library for using Blowfish crypting?


Solution

  • Use the struct module to cast to and from bytes.

    Your example would be:

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

    Reference: http://docs.python.org/3/library/struct.html