Skip to content

Python 2 methods to_bytes and from_bytes #1

@elelement

Description

@elelement

Hi, your library was very useful to me. I was able to take pictures with my code but I was having a lot of unexpected errors and the docs didn't help me so much. Your code works great!

However, I think you're using python 3, because with Python 2 your code is incompatible. The solution is to change the following calls:

  • int.from_bytes --->
def __from_bytes(self, bytes):
    return int(bytes.encode('hex'), 16)
  • int.to_bytes -->
def __to_bytes(self, n, length, byteorder='big'):
    h = '%x' % n
    s = ('0'*(len(h) % 2) + h).zfill(length*2).decode('hex')
    return s if byteorder == 'big' else s[::-1]

Good job!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions