matthew has interpreted the problem correctly, sorry if the question
wasn't clear.

I am trying to read back and debug a string of characters from a serial
port, including some outside the normal character set (eg decimal 131,
etc)

import serial #from pyserial
#set up serial port
z = ser.read()
z is a 1 length string, maybe a printable character, maybe not.. how do
I see the hex value in z?

On Thu, 28 Aug 2003 12:37:43 +1200
Matthew Gregan <[EMAIL PROTECTED]> wrote:

> On Thu, Aug 28, 2003 at 12:21:47PM +1200, Michael JasonSmith wrote:
> > On Thu, 2003-08-28 at 11:43, Nick Rout wrote:
> > > z is a one byte hex value. If I print z I get some weird character from
> > > the top end of the ascii table, which is expected. However I'd rather
> > > have the hex or decimal value,
> > > 
> > > how do I do this? something to do with a % sign and an x, but I cannot
> > > grok it at all.
> 
> > >>> f = 0xF7
> 
> But look:
> >>> type(x)
> <type 'int'>
> 
> > >>> print '%c' % f
> > ?
> > >>> print '%d' % f
> > 247
> > >>> print '%x' % f
> > f7
> 
> If I understand Nick correctly, the situation is as follows:
> 
> >>> z = "\xf7"
> 
> And see that:
> >>> type(z)
> <type 'string'>
> >>> print z
> �
> >>> print "%c" % z
> �
> >>> print "%d" % z
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> TypeError: an integer is required
> >>> print "%x" % z
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> TypeError: an integer is required
> >>> print "%d" % ord(z)
> 247
> >>> print "%c" % ord(z)
> �
> >>> print "%x" % ord(z)
> f7
> 
> Cheers,
> -mjg
> -- 
> Matthew Gregan                     |/
>                                   /|                [EMAIL PROTECTED]
> 

--
Nick Rout
Barrister & Solicitor
Christchurch, NZ
Ph +64 3 3798966
Fax + 64 3 3798853
http://www.rout.co.nz
[EMAIL PROTECTED]

Reply via email to