[Tutor] reiterative programming
Lloyd Kvam
pythontutor at venix.com
Mon Oct 27 14:45:09 EST 2003
You might find it easier to code against the integer that ord(c)
returns and delay converting to hex until you actually need to use
the hex value.
returnedval = None
while returnedval != 4:
output=port.read(1) # using 1 was suggested by someone else
returnedval= ord(output)
In those places where you need the hex value, you would specify:
hex(returnedval)
This avoids "fussy" string comparisons. (e.g. '0x4', '0X4', '0x04')
Stanfield, Vicki {D167~Indianapolis} wrote:
> 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.
>
>
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
--
Lloyd Kvam
Venix Corp.
1 Court Street, Suite 378
Lebanon, NH 03766-1358
voice: 603-653-8139
fax: 801-459-9582
More information about the Tutor
mailing list