#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    7
    Rep Power
    0

    Struct- unpacking dynamically size variables


    I am trying to unpack a payload that changes in size. However I get an error when compiling

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

    my code is here

    Code:
    if len(response['rf_data']) == 2:
         payload._make(unpack('h', response['rf_data']))
    
    if len(response['rf_data']) == 4:
        .....
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,841
    Rep Power
    480
    It's a runtime error, not a "compilation" problem.

    'h' has length 1. Is that the trouble?
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    7
    Rep Power
    0
    Originally Posted by b49P23TIvg
    It's a runtime error, not a "compilation" problem.

    'h' has length 1. Is that the trouble?
    From this page, http://docs.python.org/2/library/struct.html it looks like it is of size 2. My response['rf_data'] can change in size and it looks like Python wants it to be a static size.
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,841
    Rep Power
    480
    Maybe you provided enough information to solve the problem. For me I often need a (short) complete program. Better still is the input and expected output---then I can mostly ignore your program and write code that works.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo