[Tutor] Pack as HEX question

ALAN GAULD alan.gauld at btinternet.com
Sun Oct 25 02:20:27 CET 2009

>This is different to what I expected, it is a string of ascii bytes representing hex values.
>Is that the real format of your data? If so we need to split it into pairs and use 
>int() to convert it. Or is your real data a string of bytes that happen to have 
>those 11 hex values?
>key = struct.unpack("d", XorKey)
>You have here 8 bytes(chars) not 4 bytes. 
This is what I was talking about in my first 
message you need to be clear about exactly 
what form your data takes. Is it a string 
representing hex values or is it a sequence 
of bytes? For example the 4 hex bytes 
41,42,43,44 are represented by the 
string "ABCD" which is very different
to "41424344" which is a string of 8 bytes 
and we can see those values with:

for c in XorKey:
    print hex(ord(c))

num_ints = len(EncryptString)/11I said divide the data by the length of an int. An int is usually 4 bytes 
(you can find that by using the calcsize() function in the struct module)
so you want to divide EncryptString by 4. But only if the data is not 
in this format. If it is in this string format you need to split the string into 2 
character pairs and feed them to int()

data = struct.unpack("%dd"% num_ints,EncryptString)
>> The above code generates an error the my string must be a string length of 16Always post full error messages do not summarize them. 
Its not clear exactl;y which string the error is complaining 
about nor why.
I suspect you have a value of 2 for num_ints so your unpack is looking 
for 8 bytes but you are giving it 22. So where the 16 is coming from 
I'm not sure.

> When you say I need to determine the number of ints in my data 
>> i.e. EncryptString that value would be 11 right?Your string is 22 characters (i.e. bytes) long. It represents 11 bytes in hex which is

3 ints plus 3 bytes left over.
You need to really stop and check what kind of data you are dealing with.
In this case its strings, but in your real world case it may be real bytes in 
which case your test code is invalid. Understanding the data is critical in 
this kind of scenario.

Alan G.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091025/a7f692fd/attachment-0001.htm>

More information about the Tutor mailing list