(unknown)

Steve Holden steve at holdenweb.com
Fri Apr 11 09:07:42 EDT 2008


ha bo wrote:
> hi i use this programme in my application django:
> import struct
> MASK_CCITT  = 0x1021     # CRC-CCITT mask (ISO 3309, used in X25, HDLC)
> MASK_CRC16  = 0xA001     # CRC16 mask (used in ARC files)
> 
> def updcrc(crc, data, mask=MASK_CRC16):
> 
>      data_length = len(data)
>      unpackFormat = '%db' % data_length
>      unpackedData = struct.unpack(unpackFormat, data)
> 
>      for char in data:
>             c = ord(char)
>             c = c << 8
>    
>             for j in xrange(8):
>                 if (crc ^ c) & 0x8000:
>                     crc = (crc << 1) ^ mask
>                 else:
>                     crc = crc << 1
>                 c = c << 1
> 
>      return crc & 0xffff
> 
> 
> and i call this function in other function in my view:
> 
> 
> def encodekey(var, expires=None, user='', trusted=False):
>  
>         import random, base64
>         import updcrc
>         key = "%02X" % len(var) + var
>         key += "%02X" % len(user) + user
>         if expires is not None:
>             key += expires.strftime('%Y%m%d%H%M')
>         else:
>             year = random.choice(range(2000,2100))
>             month = 23
>             day = random.choice(range(1,32))
>             hour = random.choice(range(1,25))
>             minute = random.choice(range(1,60))
>             key += "%04d%02d%02d%02d%02d" % (year, month, day, hour, minute)
> 
>         if trusted:
>             checksum = updcrc(42, key)
>         else:   
>             checksum = updcrc(0, key)
>         key += "%04X" % checksum
> 
>         return base64.b64encode(key)
> but it give me this error:
> 
> 
>     unpack requires a string argument of length 31
> 
> 
> someone can help me
> 
Next time you report an error, please include the whole traceback, not 
just the exception message. That information is included for a reason.

You might also want to print out the value of data in your updcrc 
function, since that is where the problem is occurring.

Finally I might point out there is little point to the struct.unpack 
since you don't use its result, but I am guessing this is because your 
algorithm is currently in transition.

regards
  Steve
-- 
Steve Holden        +1 571 484 6266   +1 800 494 3119
Holden Web LLC              http://www.holdenweb.com/




More information about the Python-list mailing list