[Tutor] Struct the solution for Hex translation

Johan Geldenhuys johan at accesstel.co.za
Mon Feb 19 16:25:25 CET 2007


 Here is what I have:

>>> data
'\xa5\x16\x0b\x0b\x00\xd5\x01\x01\x01\x00\x00\xe3\x84(\x01\xc6\x00\x00\x17\x
01C\xc7'
>>> data[0]
'\xa5'
>>> len(data[0])
1
>>>

You see that data[0] is only one byte and it doesn't see all four
characters.

If I want to do this:

>>> int(data[0], 16)
  File "<console>", line 1, in ?
''' exceptions.ValueError : invalid literal for int(): ¥ '''


But I can do this:

>>> int('a5', 16)
165
>>>

If I use data[0] as it is, I get errors. That why I want to know how I can
strip away the '\x'.

Here is some other code to convert Hex to Binary:

hex2bin = {
"0" : "0000", "1" : "0001", "2" : "0010", "3" : "0011", 
"4" : "0100", "5" : "0101", "6" : "0110", "7" : "0111", 
"8" : "1000", "9" : "1001", "a" : "1010", "b" : "1011", 
"c" : "1100", "d" : "1101", "e" : "1110", "f" : "1111"
}

>>> def hexBin(hexchars):
...     s = ""
        for hexchar in hexchars:
            s += hex2bin[hexchar]
        return s.rstrip("\n")
... 
>>> hexBin('a5')
'10100101'

This however does not work if my argument is '\xa5'.

>>> hexBin('\xa5')
  File "<console>", line 1, in ?
  File "<console>", line 5, in hexBin
''' exceptions.KeyError : '\xa5' '''
>>> 

Thanks

Johan

-----Original Message-----
From: tutor-bounces at python.org [mailto:tutor-bounces at python.org] On Behalf
Of Alan Gauld
Sent: 19 February 2007 05:04 PM
To: tutor at python.org
Subject: Re: [Tutor] Struct the solution for Hex translation


"Johan Geldenhuys" <johan at accesstel.co.za> wrote 

> The first two bytes of the data is a 16 bit value. Eg: "\xe2\x01'
> 
> I can the first byte into binary if I use 'e2', but I don't know how 
> to get the '\x' out of the first byte to use it in python.

Are you sure it is there?
Usually the \x is only part of the repr string, its not actually in the
data. What do you get is you do:

byte = data[0]  # get the first byte
print len(byte)   # should only be one byte
print byte     # should get '\xe2' or whatever.

> My data has the '\x' and all I need is the 'e2'.

If you do have the \xe2  that implies you have 4 characters, ie 4 bytes, so
to get the real value use int(data[2:],16)

> Any advice of the usage of the struct module or how I can get rid of 
> the '\x' in my data?

I'm not sure where the struct module comes in? Are you using struct to read
the data? If so you should be able to use unpack the data into the format
you need by specifying a format string.

eg

struct.unpack('cc5s',data)

Should return two characters(bytes) and a 98 character 
string. Like so:

>>> struct.unpack('cc5s','\x12\x23abcde')
('\x12', '#', 'abcde')
>>>

Is that what you want?
Notice that the first value is actially a single byte of 
value 12 hex. The \x are only in the display.

The >>> prompt is a great place to experiment with struct
format strings etc.

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld

_______________________________________________
Tutor maillist  -  Tutor at python.org
http://mail.python.org/mailman/listinfo/tutor

-- 
No virus found in this incoming message.
Checked by AVG Free Edition.
Version: 7.5.441 / Virus Database: 268.18.2/692 - Release Date: 2007/02/18
04:35 PM
 

-- 
No virus found in this outgoing message.
Checked by AVG Free Edition.
Version: 7.5.441 / Virus Database: 268.18.2/692 - Release Date: 2007/02/18
04:35 PM
 



More information about the Tutor mailing list