Velocity Reviews > [Fwd: Re: hex string to hex value]

# [Fwd: Re: hex string to hex value]

tim
Guest
Posts: n/a

 11-22-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)

http://www.velocityreviews.com/forums/(E-Mail Removed) 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
>
>
>

Rocco Moretti
Guest
Posts: n/a

 11-23-2005
tim wrote:
> 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)

>>> note = 0x42
>>> print note

66
>>> note is 66

True

There is no such thing as a "hex value"- only hex *representations* of a
value.

midi.note_off() doesn't take a "hex value", it takes an integer, and,
for whatever reason, it happens to be listed in your example in a
hexidecimal representation.

Dennis Lee Bieber
Guest
Posts: n/a

 11-23-2005
On Wed, 23 Nov 2005 00:48:37 +0100, tim <(E-Mail Removed)> declaimed
the following in comp.lang.python:

> > 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)
>

There's no difference. 0x42 and 66 are TEXT REPRESENTATIONS of
integer values that are stored internally as binary. It may only become
critical if you need an exact BYTE (which would be a 1-character string
in Python)
--
> ================================================== ============ <
> (E-Mail Removed) | Wulfraed Dennis Lee Bieber KD6MOG <
> (E-Mail Removed) | Bestiaria Support Staff <
> ================================================== ============ <
> Overflow Page: <http://wlfraed.home.netcom.com/> <