[Tutor] More confusion on conversion

Stanfield, Vicki {D167~Indianapolis} VICKI.STANFIELD at ROCHE.COM
Thu Oct 30 15:24:03 EST 2003


Nope, this write '0A' out one the port. I need to write the hex value of the 0 and then the hex value of the 'A'. WHen I tried this:

for each in [ hex(ord(x)) for x in '%02X' % length ]:
                    port.write('%02X' %each)
                    time.sleep(.01)
                    print '%02X' %each

It was close, but it wants an integer instead of the hex values.

port.write('%02X' %each)
TypeError: an integer is required

--vicki

-----Original Message-----
From: Reggie Dugard [mailto:reggie at merfinllc.com]
Sent: Thursday, October 30, 2003 3:09 PM
To: Stanfield, Vicki {D167~Indianapolis}
Cc: tutor at python.org
Subject: RE: [Tutor] More confusion on conversion


Vicki,

If I understand you correctly you simply want something like:

	port.write('%02X' % length)

This should write 2 bytes on the port, one for each hex digit.  If I'm
misunderstanding what you want, maybe you can clarify it a bit more.

Also, I'm sending this back to the list so that people more
knowledgeable than me can help you out as well.

Good luck with your problem.

Reggie

On Thu, 2003-10-30 at 11:23, Stanfield, Vicki {D167~Indianapolis} wrote:
> Pardon my stupidity here, but how does one write this so that I can insert it into my code. It does just what I want, but I need to write those individual values out on the serial port. I tried:
> 
> for each in [ hex(ord(x)) for x in '%02X' % length ]:
>                     port.write(each)
>                     
> but that wrote the 30 78 30 30 for the 0x00 and 30 78 30 38 for the 0x08. What I need is to write just the hex 30 and then a hex 38. Can you give me a lesson? I tried 
> 
> for each in [ hex(ord(x)) for x in '%02X' % length ]:
>                     new=string.replace(each,'0','\')
>                     port.write(new)
>                     time.sleep(.01)
>                     print new
> 
> But I get an invalid token error on the close parenthesis in the string replace statement.
> 
> --vicki
> 
> -----Original Message-----
> From: Reggie Dugard [mailto:reggie at merfinllc.com]
> Sent: Thursday, October 30, 2003 1:35 PM
> To: Vicki Stanfield
> Cc: tutor at python.org
> Subject: Re: [Tutor] More confusion on conversion
> 
> 
> Ok I won't ask :-).  How about something like:
> 
> >>> length = 10
> >>> [ hex(ord(x)) for x in '%02X' % length ]
> ['0x30', '0x41']
> >>>
> 
> Reggie
> 






More information about the Tutor mailing list