Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   hex string to hex value (http://www.velocityreviews.com/forums/t351810-hex-string-to-hex-value.html)

tim 11-22-2005 11:15 PM

hex string to hex value
 
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: required argument is not an integer

>>> int(n)

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!


avnit 11-22-2005 11:22 PM

Re: hex string to hex value
 
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))



mensanator@aol.com 11-22-2005 11:33 PM

Re: hex string to hex value
 

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 11-22-2005 11:34 PM

Re: hex string to hex value
 
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)
>>>>
>>>>

>
>in your case it would be:
>
>
>
>>>>m=66
>>>>n=int(hex(m))
>>>>
>>>>

>
>
>




mensanator@aol.com 11-22-2005 11:56 PM

Re: hex string to hex value
 

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

>>> int(hex(m),0)

66

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

>>> int('0x1A',0)

26
>>> int('0x1A',16)

26
>>> int('1A',16)

26
>>> int('1A',0)


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



> thank you
> Tim
>
> avnit wrote:
>
> >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))
> >>>>
> >>>>

> >
> >
> >



tim 11-23-2005 12:17 AM

Re: hex string to hex value
 

mensanator@aol.com wrote:

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


Grant Edwards 11-23-2005 01:33 AM

Re: hex string to hex value
 
On 2005-11-23, tim <tim.vets@skynet.be> 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 a hex string to an integer object.

--
Grant Edwards grante Yow! It's OKAY --- I'm an
at INTELLECTUAL, too.
visi.com

Fredrik Lundh 11-23-2005 07:04 AM

Re: hex string to hex value
 
mensanator@aol.com 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
> >>> int('0x1A',16)

> 26
> >>> int('1A',16)

> 26
> >>> int('1A',0)

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




mensanator@aol.com 11-23-2005 06:27 PM

Re: hex string to hex value
 

Fredrik Lundh wrote:
> mensanator@aol.com 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
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?

>
> > (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
> > >>> int('0x1A',16)

> > 26
> > >>> int('1A',16)

> > 26
> > >>> int('1A',0)

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


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?

>
> </F>




All times are GMT. The time now is 06:06 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.