[Fwd: Re: hex string to hex value]

tim tim.vets at skynet.be
Tue Nov 22 18:48:37 EST 2005


ok, but if i do

 >>> n=66
 >>> m=hex(n)
 >>> m
'0x42'
 >>> h=int(m,16)
 >>> h
66
 >>>

I end up with 66 again, back where I started, a decimal, right?
I want to end up with 0x42 as being a hex value, not a string, so i can 
pas it as an argument to a function that needs a hex value.
(i am trying to replace the 0x42 in the line  midi.note_off(channel=0, 
note=0x42) with a variable)


mensanator at aol.com wrote:

> avnit wrote:
>  
>
>> If you just want to convert a string to an integer, it would be:
>>
>>   
>>
>>>>> int(n)
>>>>>         
>>>>
>
> That's what the OP tried and it didn't work.
>
> BECAUSE you have to tell the int function what base the string is in
> (even though it has "0x" at the start).
>
>  
>
>>>> int(n,16)
>>>>       
>>>
> 66
>
>  
>
>> in your case it would be:
>>
>>   
>>
>>>>> m=66
>>>>> n=int(hex(m))
>>>>>         
>>>>
>
> Wrong again, you still have to tell it what base.
>
>  
>
>>>> h=int(hex(m),16)
>>>> h
>>>>       
>>>
> 66
>
>  
>






More information about the Python-list mailing list