473,396 Members | 1,784 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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?

Thank you in advance.
Stefan

Nov 22 '05 #1
16 3709
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?

Thank you in advance.
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?

Thank you in advance.
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?

Thank you in advance.
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?

Thank you in advance.
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?

Thank you in advance.
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?

Thank you in advance.
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
eradicate.
--
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
eradicate.
--
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.

Similar topics

4
by: Joe Peterson | last post by:
I could not find another example of this via internet searches, so here it is... I am wondering if this is a python bug or otherwise. The first example of this happened in a larger program of...
1
by: Num | last post by:
Hi all, I have to convert a J2EE date as a long ("Millis") in a .NET date as a long ("Ticks") In Java, currentTimeMillis, is the difference, measured in milliseconds, between the current time...
17
by: Adam Ierymenko | last post by:
I have a question that might have been asked before, but I have not been able to find anything via groups.google.com or any web search that is definative. I am writing an evolutionary AI...
8
by: Jerry | last post by:
I have an off-the-shelf app that uses an Access database as its backend. One of the tables contains a field with an "OLE Object" datatype. I'm writing some reports against this database, and I...
0
by: ondekoza | last post by:
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
12
by: Zero | last post by:
Hi everybody, i want to write a small program, which shows me the biggest and smallest number in dependance of the data type. For int the command could be: ...
0
by: RON | last post by:
I have to read a long text field (it's a PDF) from SQL Server in a VB.NET (2003) app. Here's the code I have: Sub GetPDF(byval Path as string) 'objConnection created elsewhere; not the problem...
0
by: RON | last post by:
have to read a long text field (it's a PDF stored in a column) from SQL Server in a VB.NET (2003) app. Here's the code I have: Sub GetPDF(byval Path as string) 'objConnection created elsewhere;...
0
by: Curious | last post by:
Hi, I have two columns defined as DateTime type in the Visual Designer, i.e., Dataset.xsd. In the grid to which the columns are bound, they're both displayed as date, for instance, 5/23/2007....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.