Fredrik Lundh wrote:
[EMAIL PROTECTED] wrote:
Fredrik Lundh's solution works if the hex string starts with 0x
that's what interpret [it] as a Python literal meant.
I know from personal experience that the implications of that
sometimes go right over the head of a newbie. Did I do something
This is probably another newbie question...but...
even after reading quite some messages like '..hex to decimal',
'creating a hex value' , I can't figure this out:
If i do
m=66
n=hex(m)
n
'0x42'
i cannot use n as value for a variable that takes hex values, because it
throws:
error:
If you just want to convert a string to an integer, it would be:
int(n)
in your case it would be:
m=66
n=int(hex(m))
--
http://mail.python.org/mailman/listinfo/python-list
tim [EMAIL PROTECTED] wrote:
This is probably another newbie question...but...
even after reading quite some messages like '..hex to decimal',
'creating a hex value' , I can't figure this out:
If i do
m=66
n=hex(m)
n
'0x42'
i cannot use n as value for a variable that takes hex values,
but then i get :
m
66
n=int(hex(m))
Traceback (most recent call last):
File interactive input, line 1, in ?
ValueError: invalid literal for int(): 0x42
what am I missing here ?
thank you
Tim
avnit wrote:
If you just want to convert a string to an integer, it would be:
int(n)
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
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
tim wrote:
but then i get :
m
66
n=int(hex(m))
Traceback (most recent call last):
File interactive input, line 1, in ?
ValueError: invalid literal for int(): 0x42
what am I missing here ?
Avnit's solution was wrong. When converting a string, you
must state what base you are
state what base you are converting from.
int(hex(m),16)
66
Fredrik Lundh's solution works if the hex string starts with 0x
(which it will when the string is created with the hex function).
aren't you converting from a hex string to a decimal value here?
--
http
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
tim wrote:
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
Brett g Porter wrote:
tim wrote:
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
On 2005-11-23, tim [EMAIL PROTECTED] wrote:
int(hex(m),16)
66
Fredrik Lundh's solution works if the hex string starts with 0x
(which it will when the string is created with the hex function).
aren't you converting from a hex string to a decimal value here?
No.
He's converting from
[EMAIL PROTECTED] wrote:
Fredrik Lundh's solution works if the hex string starts with 0x
that's what interpret [it] as a Python literal meant.
(which it will when the string is created with the hex function).
int(hex(m),0)
66
But it won't work without the 0x.
int('0x1A',0)
26
14 matches
Mail list logo