[Tutor] reiterative programming

Stanfield, Vicki {D167~Indianapolis} VICKI.STANFIELD at ROCHE.COM
Mon Oct 27 13:59:51 EST 2003


No, I want the hex version of the ASCII representation. It is what the item on the other end wants. I don't know why they do it that way, but if I want to talk to their device, I have to do it that way. At least you give me hope that the awkwardness I perceive is not simply my perception.

--vicki

-----Original Message-----
From: Alan Gauld [mailto:alan.gauld at blueyonder.co.uk]
Sent: Monday, October 27, 2003 1:52 PM
To: Stanfield, Vicki {D167~Indianapolis}; tutor at python.org
Subject: Re: [Tutor] reiterative programming


I'm not sure I understand the logic of the conversion.

hex(ord(c))

takes the character c, converts it to a number equal
to its ASCII value and then turns that number into a
hex representation. (Try it in the interpreter:

>>> print ord('4')
52
>>> print hex(ord(4))
0x34

Are you sure you don't want the hex representation of
the number the character represents

>>> print hex(int('4'))
0x4

Or even

>>> print int('4',16)
4

> I want to loop through doing this until the returned value is a hex
4.
>
> while returnedval != '\0x4':

This checks for the string '\0x4' which treats the \ as an escape
and yields:.

>>> print '\0x4'
 x4

You could use a raw string:

>>> print r'\0x4'
\0x4

> For some reason that I don't understand, when the returnedval
> is '0x4', it does the loop again

Because '0x4' is not the same as '\0x4'!

You need to be very clear about what you actually want to compare
with what, and then check the actual format you are using.
The >>> prompt is your friend.

Alan G.





More information about the Tutor mailing list