[Tutor] conversion confusion

Lloyd Kvam pythontutor at venix.com
Tue Sep 30 15:07:41 EDT 2003


hex takes a numeric input and returns a string with the hex characters to
represent that number.
 >>> hex(1234)
'0x4d2'
 >>> hex(ord('m'))
'0x6d'

Just use:
	ord(input_character) ^ seed
 >>> seed = 1234
 >>> ord('m') ^ seed
1215

Note that you probably do not need to convert the character to hex (except
for display purposes.
 >>> ord('m')
109
 >>> hex(ord('m'))
'0x6d'
 >>> int('6d',16)
109


Stanfield, Vicki {D167~Indianapolis} wrote:

> Now that I think about it, it is not really a hex value when I pass it in but a string. I pass either a number or an alpha character like 'm' that I read from the command line. I need to convert whatever it is to a hex value. For instance, 'm' would be converted to '6D' before being XOR'ed to the seed value. When I try to use hex('m'), I am told that I can't convert a hex value to a hex value. How is 'm' a hex value?
> 
> --vicki 
> 
> -----Original Message-----
> From: Lloyd Kvam [mailto:pythontutor at venix.com]
> Sent: Tuesday, September 30, 2003 12:04 PM
> To: Vicki Stanfield
> Cc: tutor at python.org
> Subject: Re: [Tutor] conversion confusion
> 
> 
>  >>> ord('m')
> 109
>  >>> int('6d',16)
> 109
> 
> So int(value2add,16) should give you the right number.
> 
> I assume this relates to the serial processing that you've written about in
> the past.  Did you create value2add by using a hex funtion on a character?
> Would it be feasible to simply use ord on the original character?
> 
> Vicki Stanfield wrote:
> 
> 
>>I am trying to OR two values. These values are the hex values minus the 0x
>>part. If I had an ASCII 'm' for instance, I would OR the value 6D to the
>>existing seed value. When I try this, I get an error.
>>
>>CRCval=ord(value2add) ^ ord(seed)
>>
>>because the value2add is 2 characters rather than one. Is there an
>>established function to take a multi-character string and convert it to a
>>numeric value that works with OR?
>>
>>--vicki
>>
>>_______________________________________________
>>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-443-6155
fax:	801-459-9582




More information about the Tutor mailing list