Error unpack requires a string argument of length 16

Prometheus

Using Python 2.7 I had this code:

#Create a random 56 bit (7 byte) string
random_bytes = os.urandom(7)
random_bytes = int.from_bytes(random_bytes, byteorder="big")

Which gives the error:

AttributeError: type object 'int' has no attribute 'from_bytes'

After reading online it looks like from_bytes is Python 3. So I tried the following:

  random_bytes = os.urandom(7)
  #random_bytes = int.from_bytes(random_bytes, byteorder="big")
  random_bytes = struct.unpack('>16B', random_bytes)

But this gives the following error:

struct.error: unpack requires a string argument of length 16

Should the >16B be >7B? Even with that it seems to return a tuple.

The goal is to use random_bytes like this:

int(str(((time_bytes << 56) | random_bytes)) + code)
thebjorn

Maybe this will work for you:

r = chr(0) + os.urandom(7)  # pad with zeroes
struct.unpack('>q', r)

q being the code for signed 8-byte integer (Q is unsigned).

The result is a tuple with one element.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

unpack requires a string argument of length 24

分類Dev

Error when printing string.length

分類Dev

struct.error:unpackには長さ16の文字列引数が必要です

分類Dev

tar: option requires an argument -- 'f'

分類Dev

Python - Unpack lists and join into a string

分類Dev

Python - Unpack lists and join into a string

分類Dev

The length of a string

分類Dev

Use gsub to remove pattern from a string: argument 'pattern' has length > 1 and only the first element will be used

分類Dev

Receiving error: argument of length 0 when reading in separate tables from multiple csv files in for loop

分類Dev

Odd-length string error with binascii.unhexlify

分類Dev

How to fix Argument of type '"email"' is not assignable to parameter of type 'string[]' error?

分類Dev

Java multiple variable length argument

分類Dev

Retrieve the length of variable argument list

分類Dev

How to unpack a list of tuple in various length in a panda dataframe?

分類Dev

How can I unpack an arbitrary length list of IO Bool

分類Dev

I meet an error with Material-UI and Mobx ,Error: Material-UI: capitalize(string) expects a string argument

分類Dev

DPKG: Error Processing (--unpack) (RaspberryPi Compiler - Ubuntu)

分類Dev

Swift Error Cannot convert value of type '[String.Element]' (aka 'Array<Character>') to expected argument type '[String]'

分類Dev

Manually compute the length of a string

分類Dev

String length data for DynamoDB

分類Dev

Length in bytes of a String

分類Dev

Replace a string of defined length

分類Dev

String length always 0

分類Dev

Argument parsing inside a string

分類Dev

Overriding with object or string as argument

分類Dev

Passing a template which requires a comma to a single-argument macro

分類Dev

Not using a class in python, but my function requires a "self" argument?

分類Dev

std :: length_error:basic_stringwstringをstringに変換

分類Dev

Error testing python code: TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'

Related 関連記事

  1. 1

    unpack requires a string argument of length 24

  2. 2

    Error when printing string.length

  3. 3

    struct.error:unpackには長さ16の文字列引数が必要です

  4. 4

    tar: option requires an argument -- 'f'

  5. 5

    Python - Unpack lists and join into a string

  6. 6

    Python - Unpack lists and join into a string

  7. 7

    The length of a string

  8. 8

    Use gsub to remove pattern from a string: argument 'pattern' has length > 1 and only the first element will be used

  9. 9

    Receiving error: argument of length 0 when reading in separate tables from multiple csv files in for loop

  10. 10

    Odd-length string error with binascii.unhexlify

  11. 11

    How to fix Argument of type '"email"' is not assignable to parameter of type 'string[]' error?

  12. 12

    Java multiple variable length argument

  13. 13

    Retrieve the length of variable argument list

  14. 14

    How to unpack a list of tuple in various length in a panda dataframe?

  15. 15

    How can I unpack an arbitrary length list of IO Bool

  16. 16

    I meet an error with Material-UI and Mobx ,Error: Material-UI: capitalize(string) expects a string argument

  17. 17

    DPKG: Error Processing (--unpack) (RaspberryPi Compiler - Ubuntu)

  18. 18

    Swift Error Cannot convert value of type '[String.Element]' (aka 'Array<Character>') to expected argument type '[String]'

  19. 19

    Manually compute the length of a string

  20. 20

    String length data for DynamoDB

  21. 21

    Length in bytes of a String

  22. 22

    Replace a string of defined length

  23. 23

    String length always 0

  24. 24

    Argument parsing inside a string

  25. 25

    Overriding with object or string as argument

  26. 26

    Passing a template which requires a comma to a single-argument macro

  27. 27

    Not using a class in python, but my function requires a "self" argument?

  28. 28

    std :: length_error:basic_stringwstringをstringに変換

  29. 29

    Error testing python code: TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'

ホットタグ

アーカイブ