[Tutor] More confusion on conversion

Lloyd Kvam pythontutor at venix.com
Thu Oct 30 17:02:49 EST 2003


I assume that you need to send an even number of hex characters.  I think this
code should do the trick:

length = 1020	# 3 hex digits
hexstr = '%02X' % length
if len(hexstr) % 2:	# pad to even number of digits
     hexstr = '0' + hexstr
port.write(hexstr)
print hexstr

If multiple characters go out too quickly, restore the timing loop like
before:
for x in hexstr:
     port.write(x)
     time.sleep(.01)
     print '%02X' % ord(x)

Lloyd Kvam wrote:

> for each in [ hex(ord(x)) for x in '%02X' % length ]:
> 
> I used that line in my earlier email.  BUT it can be simplified:
> 
> length = 10
> for x in '%02X' % length:
>     port.write(x)
>     time.sleep(.01)
>     print '%02X' % ord(x)
> 
> 
> Note that '\x30' has the same value as '0'
> 
>  >>> print '\x30' == '0'
> 1
> 
> They are just alternate means of writing the same thing.  The '\x30'
> emphasizes the bit value.  The '0' is for people to read.
> 
> Stanfield, Vicki {D167~Indianapolis} wrote:
> 
>> Probably I am the one being dense, but I know that the device on the 
>> other end expects what it calls a "hex value represented as ASCII." 
>> This means that if I want to send a decimal 10, I first have to 
>> convert decimal 10 to hex 'A' and then I have to take the hex of the 
>> letter 'A' as if it were ASCII and send that. In this example, I need 
>> to send 0x30 0x41 (as two bytes of data). In this example, doing this:
>>
>> port.write('\x30')
>> port.write('\x41')
>>
>> is the equivalent of what I want. Of course, I have to make it handle 
>> lengths other than 10 too.
>>
>> Is that any clearer?
>>
>> --vicki
>>
>> -----Original Message-----
>> From: Reggie Dugard [mailto:reggie at merfinllc.com]
>> Sent: Thursday, October 30, 2003 3:47 PM
>> To: Stanfield, Vicki {D167~Indianapolis}
>> Cc: tutor at python.org
>> Subject: RE: [Tutor] More confusion on conversion
>>
>>
>> Vicki,
>>
>> Forgive me if I'm being dense, but how many bytes do you want to write
>> out on the port for each length?  If the answer is 2, then writing '0'
>> and 'A' is equivalent to writing 0 and 65 which is equivalent to writing
>> 0x00 and 0x41 - they all have the same bit pattern: 00000000 01000001.
>>
>> Sorry if I'm misunderstanding you.
>>
>> Reggie
>>
>> On Thu, 2003-10-30 at 12:24, Stanfield, Vicki {D167~Indianapolis} wrote:
>>
>>> 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
>>
>>
>>
>>
>>
>>
>> _______________________________________________
>> Tutor maillist  -  Tutor at python.org
>> http://mail.python.org/mailman/listinfo/tutor
>>
>> _______________________________________________
>> 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