hex string to hex value

T

tim

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'0x42'
i cannot use n as value for a variable that takes hex values, because it
throws:

error: required argument is not an integer
gives me:
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
ValueError: invalid literal for int(): 0x42

how do I convert '0x42' to 0x42 ?

thanks!
 
A

avnit

If you just want to convert a string to an integer, it would be:

in your case it would be:
 
M

mensanator

avnit said:
If you just want to convert a string to an integer, it would be:

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


in your case it would be:

Wrong again, you still have to tell it what base.
66
 
T

tim

but then i get :
Traceback (most recent call last):

what am I missing here ?
thank you
Tim
 
M

mensanator

tim said:
but then i get :

Traceback (most recent call last):


what am I missing here ?

Avnit's solution was wrong. When converting a string, you
must state what base you are converting from.
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).
66

But it won't work without the "0x".

Traceback (most recent call last):
File "<pyshell#18>", line 1, in -toplevel-
int('1A',0)
ValueError: invalid literal for int(): 1A
 
T

tim

tim wrote:



Avnit's solution was wrong. When converting a string, you
must state what base you are converting from.



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

Fredrik Lundh

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

66

But it won't work without the "0x".


Traceback (most recent call last):
File "<pyshell#18>", line 1, in -toplevel-
int('1A',0)
ValueError: invalid literal for int(): 1A

as the error message says, 1A isn't a valid literal.

</F>
 
M

mensanator

Fredrik said:
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
wrong by pointing out exactly what that means? Isn't it better to
have the OP understand the problem than simply solve it for him?
as the error message says, 1A isn't a valid literal.

Yes, not all strings of valid hex characters are valid literals.
But surely you're not suggesting that the user simply give
up when faced with that situation?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top