Re: How to convert a "long in a string" to a "long"?

2005-11-20 Thread ondekoza
Thank you, for all the good answers. Somehow I overlooked the 'radix'
option in the docs.

S

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to convert a "long in a string" to a "long"?

2005-11-18 Thread Steven D'Aprano
On Fri, 18 Nov 2005 17:49:50 +, Leif K-Brooks wrote:

> Sion Arrowsmith wrote:
>> Steven Bethard  <[EMAIL PROTECTED]> wrote:
>> 
>>>[EMAIL PROTECTED] wrote:
>>>
s = long("0xL")
ValueError: invalid literal for long(): 0xL

>>int("0x", 0)
>>>
>>>4294967295L
>> 
>> So why does the base argument to int() (or long()) default to
>> 10 and not 0?
> 
> Because it's designed for numbers normal people provide, not for numbers
> programmers provide. Normal people see 0123 as being equal to 123, not 83.

The base arguments to int() and long() default to base 10 because base 10
is used by just about all people and cultures in the world. Leading zeroes
are mathematically meaningless: 0123 means 0*base**3 + 1*base**2 +
2*base**1 + 3*base**0, which is identical to 123 no matter what base you
choose.

Interpreting 0123 in octal is a sop to programmers who want/need
compatibility to the C bug that changes the meaning of numeric literals
according to the presence or absence of a leading zero. Alas I suspect
that this particular piece of illogic is too ingrained now to ever
eradicate.


-- 
Steven.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to convert a "long in a string" to a "long"?

2005-11-18 Thread Steven D'Aprano
On Fri, 18 Nov 2005 11:10:06 -0500, Carsten Haese wrote:

>> s = long("0xL")
>> ValueError: invalid literal for long(): 0xL
>> 
>> s = long("0x")
>> ValueError: invalid literal for long(): 0xL
>> 
>> What can I do? 
>> 
>> Thank you in advance.
>> Stefan
> 
> Leave out the "0x" prefix and tell long() that you're using base 16:
> 
 long("", 16)
> 4294967295L


Or leave the prefix in, and tell Python to use the prefix to predict the
base:

py> long("0xL", 0)
4294967295L



-- 
Steven.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to convert a "long in a string" to a "long"?

2005-11-18 Thread Leif K-Brooks
Sion Arrowsmith wrote:
> Steven Bethard  <[EMAIL PROTECTED]> wrote:
> 
>>[EMAIL PROTECTED] wrote:
>>
>>>s = long("0xL")
>>>ValueError: invalid literal for long(): 0xL
>>>
>int("0x", 0)
>>
>>4294967295L
> 
> So why does the base argument to int() (or long()) default to
> 10 and not 0?

Because it's designed for numbers normal people provide, not for numbers
programmers provide. Normal people see 0123 as being equal to 123, not 83.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to convert a "long in a string" to a "long"?

2005-11-18 Thread Sion Arrowsmith
Steven Bethard  <[EMAIL PROTECTED]> wrote:
>[EMAIL PROTECTED] wrote:
>> s = long("0xL")
>> ValueError: invalid literal for long(): 0xL
> >>> int("0x", 0)
>4294967295L

So why does the base argument to int() (or long()) default to
10 and not 0?

-- 
\S -- [EMAIL PROTECTED] -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: How to convert a "long in a string" to a "long"?

2005-11-18 Thread Steven Bethard
[EMAIL PROTECTED] wrote:
0xL
> 
> 4294967295L
> 
> OK, this is what I want, so I tried
> 
> s = long("0xL")
> ValueError: invalid literal for long(): 0xL

 >>> int("0x", 0)
4294967295L

STeVe
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to convert a "long in a string" to a "long"?

2005-11-18 Thread Klaus Alexander Seistrup
Carsten Haese wrote:

>> OK, this is what I want, so I tried
>> 
>> s = long("0xL")
>> ValueError: invalid literal for long(): 0xL
>> 
>> s = long("0x")
>> ValueError: invalid literal for long(): 0xL
>> 
>> What can I do? 
>> 
>> Thank you in advance.
>> Stefan
>
> Leave out the "0x" prefix and tell long() that you're using 
> base 16:
>
 long("", 16)
> 4294967295L

It's sufficient to tell long() that you're using base 16:

#v+

>>> long('0xL', 16)
65535L
>>> 

#v-

Cheers,

-- 
Klaus Alexander Seistrup
Copenhagen, Denmark
http://seistrup.dk/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to convert a "long in a string" to a "long"?

2005-11-18 Thread Carsten Haese
On Fri, 2005-11-18 at 11:04, [EMAIL PROTECTED] wrote:
> Hello,
> 
> I need to convert the string "" to a long. To convert this
> string I tried the following:
> >>> 0x
> -1
> >>> 0xL
> 4294967295L
> 
> OK, this is what I want, so I tried
> 
> s = long("0xL")
> ValueError: invalid literal for long(): 0xL
> 
> s = long("0x")
> ValueError: invalid literal for long(): 0xL
> 
> What can I do? 
> 
> Thank you in advance.
> Stefan

Leave out the "0x" prefix and tell long() that you're using base 16:

>>> long("", 16)
4294967295L

HTH,

Carsten.

-- 
http://mail.python.org/mailman/listinfo/python-list


How to convert a "long in a string" to a "long"?

2005-11-18 Thread ondekoza
Hello,

I need to convert the string "" to a long. To convert this
string I tried the following:
>>> 0x
-1
>>> 0xL
4294967295L

OK, this is what I want, so I tried

s = long("0xL")
ValueError: invalid literal for long(): 0xL

s = long("0x")
ValueError: invalid literal for long(): 0xL

What can I do? 

Thank you in advance.
Stefan

-- 
http://mail.python.org/mailman/listinfo/python-list