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

Discussion in 'Python' started by tim, Nov 22, 2005.

1. ### timGuest

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)

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

tim, Nov 22, 2005

2. ### Rocco MorettiGuest

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.

Rocco Moretti, Nov 23, 2005

3. ### Dennis Lee BieberGuest

On Wed, 23 Nov 2005 00:48:37 +0100, tim <> 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)
--
> ============================================================== <
> | Wulfraed Dennis Lee Bieber KD6MOG <
> | Bestiaria Support Staff <
> ============================================================== <
> Home Page: <http://www.dm.net/~wulfraed/> <
> Overflow Page: <http://wlfraed.home.netcom.com/> <

Dennis Lee Bieber, Nov 23, 2005

## Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.