[Tutor] Pack as HEX question

Alan Gauld alan.gauld at btinternet.com
Sat Oct 24 16:24:46 CEST 2009

"Tom Green" <xchimeras at gmail.com> wrote

> wondering if there was someway in Python to indicate that the string is a
> string of HEX values similar to Perl's pack.

You need to be very careful in your terminology here.

Is it "a string of hex values" ie a string representation of a hex
value or is it a string of bytes which can be represented in hex
as you showed? The two and their processing are entirely different!

> Encrypted string in hex
> "313B372C2E2C63362E2128"
> Four byte key
> XOR key "41424344"

Assuming the data really is binary data represented by the hex
value you display above then you can use the struct module to
unpack the values into integers directly.

If it is a string of characters representing hex values then you can use
the int() function to convert them by providing a base argument of 16..

> I have done this numerous times, but I wanted to throw it out there as I
> know there must be a easier way.  I am using Python 2.5.2

There is almost certainly an easier way but which way we
that is cannot tell for sure from the information given.


Alan Gauld
Author of the Learn to Program web site

More information about the Tutor mailing list