473,244 Members | 1,650 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,244 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 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?

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: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.