473,244 Members | 1,650 Online

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

Hello,

I need to convert the string "FFFFFFFF" to a long. To convert this
string I tried the following:
0xffffffff -1 0xffffffffL

4294967295L

OK, this is what I want, so I tried

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

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

What can I do?

Stefan

Nov 22 '05 #1
16 3690
On Fri, 2005-11-18 at 11:04, on******@gmail.com wrote:
Hello,

I need to convert the string "FFFFFFFF" to a long. To convert this
string I tried the following:
0xffffffff -1 0xffffffffL 4294967295L

OK, this is what I want, so I tried

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

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

What can I do?

Stefan

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

4294967295L

HTH,

Carsten.

Nov 22 '05 #2
On Fri, 2005-11-18 at 11:04, on******@gmail.com wrote:
Hello,

I need to convert the string "FFFFFFFF" to a long. To convert this
string I tried the following:
0xffffffff -1 0xffffffffL 4294967295L

OK, this is what I want, so I tried

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

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

What can I do?

Stefan

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

4294967295L

HTH,

Carsten.

Nov 22 '05 #3
Carsten Haese wrote:
OK, this is what I want, so I tried

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

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

What can I do?

Stefan

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

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

#v+
long('0xffffL', 16) 65535L

#v-

Cheers,

--
Klaus Alexander Seistrup
Copenhagen, Denmark
http://seistrup.dk/
Nov 22 '05 #4
Carsten Haese wrote:
OK, this is what I want, so I tried

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

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

What can I do?

Stefan

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

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

#v+
long('0xffffL', 16) 65535L

#v-

Cheers,

--
Klaus Alexander Seistrup
Copenhagen, Denmark
http://seistrup.dk/
Nov 22 '05 #5
on******@gmail.com wrote:
0xffffffffL
4294967295L

OK, this is what I want, so I tried

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

int("0xffffffff", 0)

4294967295L

STeVe
Nov 22 '05 #6
on******@gmail.com wrote:
0xffffffffL
4294967295L

OK, this is what I want, so I tried

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

int("0xffffffff", 0)

4294967295L

STeVe
Nov 22 '05 #7
Steven Bethard <st************@gmail.com> wrote:
on******@gmail.com wrote:
s = long("0xffffffffL")
ValueError: invalid literal for long(): 0xffffffffL
int("0xffffffff", 0)

4294967295L

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

--
\S -- si***@chiark.greenend.org.uk -- 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
Nov 22 '05 #8
Steven Bethard <st************@gmail.com> wrote:
on******@gmail.com wrote:
s = long("0xffffffffL")
ValueError: invalid literal for long(): 0xffffffffL
int("0xffffffff", 0)

4294967295L

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

--
\S -- si***@chiark.greenend.org.uk -- 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
Nov 22 '05 #9
Sion Arrowsmith wrote:
Steven Bethard <st************@gmail.com> wrote:
on******@gmail.com wrote:
s = long("0xffffffffL")
ValueError: invalid literal for long(): 0xffffffffL

>int("0xffffffff", 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.
Nov 22 '05 #10
Sion Arrowsmith wrote:
Steven Bethard <st************@gmail.com> wrote:
on******@gmail.com wrote:
s = long("0xffffffffL")
ValueError: invalid literal for long(): 0xffffffffL

>int("0xffffffff", 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.
Nov 22 '05 #11
On Fri, 18 Nov 2005 11:10:06 -0500, Carsten Haese wrote:
s = long("0xffffffffL")
ValueError: invalid literal for long(): 0xffffffffL

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

What can I do?

Stefan

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

4294967295L

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

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

--
Steven.

Nov 22 '05 #12
On Fri, 18 Nov 2005 11:10:06 -0500, Carsten Haese wrote:
s = long("0xffffffffL")
ValueError: invalid literal for long(): 0xffffffffL

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

What can I do?

Stefan

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

4294967295L

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

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

--
Steven.

Nov 22 '05 #13
On Fri, 18 Nov 2005 17:49:50 +0000, Leif K-Brooks wrote:
Sion Arrowsmith wrote:
Steven Bethard <st************@gmail.com> wrote:
on******@gmail.com wrote:

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

>>int("0xffffffff", 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
--
Steven.

Nov 22 '05 #14
On Fri, 18 Nov 2005 17:49:50 +0000, Leif K-Brooks wrote:
Sion Arrowsmith wrote:
Steven Bethard <st************@gmail.com> wrote:
on******@gmail.com wrote:

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

>>int("0xffffffff", 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
--
Steven.

Nov 22 '05 #15
Thank you, for all the good answers. Somehow I overlooked the 'radix'
option in the docs.

S

Nov 22 '05 #16
Thank you, for all the good answers. Somehow I overlooked the 'radix'
option in the docs.

S

Nov 22 '05 #17

This thread has been closed and replies have been disabled. Please start a new discussion.