[Tutor] conversion confusion
Lloyd Kvam
pythontutor at venix.com
Tue Sep 30 17:07:49 EDT 2003
XOR works on integers. The hex function takes a number and produces a
string that is meant to be easy for people to read so that they can
understand the underlying bits. The computer already understands the bits
and doesn't need any conversion.
'0x6d' represents the number 109 and allows us to recognize the bit pattern
'0x67' represents the number 103
>>> 103 ^ 109
10
>>> hex(103 ^ 109)
'0xa'
>>> int('0a',16)
10
Hex is used for people who want an aid for understanding the underlying
bits used in the computer. Generally, you only need the hex function to
display to people. The computer already understands the bits just as they
are.
Stanfield, Vicki {D167~Indianapolis} wrote:
> Now I am really confused! When I do this by hand, I put my calculator in hex mode and XOR the values 67 and 6d. It works fine there. The values I pass in are correct (67 and 6d) but I can't XOR them to save my life (Gee, I am getting old; aren't I?). If the variables value2add and seed represent 67 and 6d respectively. Why won't XOR work with them? In spite of the fact that these values are hex, the error I get is:
>
> TypeError: unsupported operand type(s) for ^: 'str' and 'str'
>
> I used binascii.hexlify to change the 'm' to 6d by the way. Why is it being seen as a string?
>
> --vicki
>
> -----Original Message-----
> From: Lloyd Kvam [mailto:pythontutor at venix.com]
> Sent: Tuesday, September 30, 2003 2:08 PM
> To: Stanfield, Vicki {D167~Indianapolis}
> Cc: Tutor Python
> Subject: Re: [Tutor] conversion confusion
>
>
> 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