How do you safely turn an arbitrarily long signed hex string into a
long integer ?
e.g. -0x55aff8080000
When treating them as hex literals I'm getting future warnings that
from 2.4 hex values greater than sys.maxint will always return
positive values. I've had to treat them as strings and write a
function to turn them into longs a character at a time ! I couldn't
find a built in function that would do this for me.
Regards,
Fuzzy http://www.voidspace.org.uk/atlantib...thonutils.html 7 4588
Michael Foord wrote: How do you safely turn an arbitrarily long signed hex string into a long integer ? e.g. -0x55aff8080000
When treating them as hex literals I'm getting future warnings that from 2.4 hex values greater than sys.maxint will always return positive values. I've had to treat them as strings and write a function to turn them into longs a character at a time ! I couldn't find a built in function that would do this for me.
One (fragile) way to avoid that Python 2.3 interprets
0x80000000...0xffffffff (on 32-bit systems) as negative integers is to add
an explicit sign:
$ python -c"print int('0xffffffff', 0)"
-c:1: FutureWarning: int('0...', 0): sign will change in Python 2.4
-1
$ python -c"print int('+0xffffffff', 0)"
4294967295
$ python -c"print int('-0xffffffff', 0)"
-4294967295
Peter
Peter Otten <__*******@web.de> wrote in message news:<cl*************@news.t-online.com>... Michael Foord wrote:
How do you safely turn an arbitrarily long signed hex string into a long integer ? e.g. -0x55aff8080000
When treating them as hex literals I'm getting future warnings that from 2.4 hex values greater than sys.maxint will always return positive values. I've had to treat them as strings and write a function to turn them into longs a character at a time ! I couldn't find a built in function that would do this for me. One (fragile) way to avoid that Python 2.3 interprets 0x80000000...0xffffffff (on 32-bit systems) as negative integers is to add an explicit sign:
Looks good. Why is it fragile ?
Regards,
Fuzzy http://www.voidspace.org.uk/atlantib...thonutils.html $ python -c"print int('0xffffffff', 0)" -c:1: FutureWarning: int('0...', 0): sign will change in Python 2.4 -1 $ python -c"print int('+0xffffffff', 0)" 4294967295 $ python -c"print int('-0xffffffff', 0)" -4294967295
Peter
The following look like they will work on Python 2.3.4:
long(float('-0x55aff8080000'))
int(float('-0x55aff8080000'))
eval('-0x55aff8080000')
Some programmers frown upon the use of eval(), so use it at your own risk.
Comments?
Rick
Michael Foord wrote: How do you safely turn an arbitrarily long signed hex string into a long integer ? e.g. -0x55aff8080000
When treating them as hex literals I'm getting future warnings that from 2.4 hex values greater than sys.maxint will always return positive values. I've had to treat them as strings and write a function to turn them into longs a character at a time ! I couldn't find a built in function that would do this for me.
Regards,
Fuzzy
http://www.voidspace.org.uk/atlantib...thonutils.html
Michael Foord wrote: One (fragile) way to avoid that Python 2.3 interprets 0x80000000...0xffffffff (on 32-bit systems) as negative integers is to add an explicit sign:
Looks good. Why is it fragile ? int("0x80000000", 0), int("+0x80000000", 0)
(-2147483648, 2147483648L)
Dunno. Is it only me feeling the itch to remove the redundant-looking +
sign?
int("-0x80000000", 0) == 0x80000000
True
Is that the equality you want to bury somewhere in a script? The fact that
the int() argument probably is a variable rather than a string constant
makes it only worse.
I'd say there is one more reason to look forward to Python 2.4.
Peter
Rick Holbert <ho******@dma.org> wrote in message news:<cl**********@charm.magnus.acs.ohio-state.edu>... The following look like they will work on Python 2.3.4:
long(float('-0x55aff8080000'))
long(float('-0x55aff8080000'))
Traceback (most recent call last):
File "<pyshell#1>", line 1, in -toplevel-
long(float('-0x55aff8080000'))
ValueError: invalid literal for float(): -0x55aff8080000
It was this error that surprised me......
int(float('-0x55aff8080000')) eval('-0x55aff8080000')
eval doesn't get round the problem at all.... eval('0xffffffff')
<string>:0: FutureWarning: hex/oct constants > sys.maxint will return
positive values in Python 2.4 and up
-1
Regards,
Fuzzy http://www.voidspace.org.uk/atlantib...thonutils.html Some programmers frown upon the use of eval(), so use it at your own risk.
Comments?
Rick
Michael Foord wrote:
How do you safely turn an arbitrarily long signed hex string into a long integer ? e.g. -0x55aff8080000
When treating them as hex literals I'm getting future warnings that from 2.4 hex values greater than sys.maxint will always return positive values. I've had to treat them as strings and write a function to turn them into longs a character at a time ! I couldn't find a built in function that would do this for me.
Regards,
Fuzzy
http://www.voidspace.org.uk/atlantib...thonutils.html
Interesting!
Here are the results from my Linux Workstation (Mandrake 10.1 on Intel):
Python 2.3.4 (#2, Aug 19 2004, 15:49:40)
[GCC 3.4.1 (Mandrakelinux (Alpha 3.4.1-3mdk)] on linux2
Type "help", "copyright", "credits" or "license" for more information. long(float('-0x55aff8080000'))
-94214268911616L
Here are the results from my school's Solaris Server (Solaris 8 on Sparc):
Python 2.3.4 (#1, Sep 24 2004, 17:33:58)
[GCC 3.0.4] on sunos5
Type "help", "copyright", "credits" or "license" for more information. long(float('-0x55aff8080000'))
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: invalid literal for float(): -0x55aff8080000
Also, here are the results from Jython on the same server:
Jython 2.1 on java1.4.0_01 (JIT: null)
Type "copyright", "credits" or "license" for more information. long(float('-0x55aff8080000'))
Traceback (innermost last):
File "<console>", line 1, in ?
ValueError: invalid literal for __float__: -0x55aff8080000
Michael Foord wrote: Rick Holbert <ho******@dma.org> wrote in message news:<cl**********@charm.magnus.acs.ohio-state.edu>... The following look like they will work on Python 2.3.4:
long(float('-0x55aff8080000')) long(float('-0x55aff8080000')) Traceback (most recent call last): File "<pyshell#1>", line 1, in -toplevel- long(float('-0x55aff8080000')) ValueError: invalid literal for float(): -0x55aff8080000 It was this error that surprised me......
int(float('-0x55aff8080000')) eval('-0x55aff8080000')
eval doesn't get round the problem at all.... eval('0xffffffff')
<string>:0: FutureWarning: hex/oct constants > sys.maxint will return positive values in Python 2.4 and up -1
Regards,
Fuzzy http://www.voidspace.org.uk/atlantib...thonutils.html
Some programmers frown upon the use of eval(), so use it at your own risk.
Comments?
Rick
Michael Foord wrote:
> How do you safely turn an arbitrarily long signed hex string into a > long integer ? > e.g. -0x55aff8080000 > > When treating them as hex literals I'm getting future warnings that > from 2.4 hex values greater than sys.maxint will always return > positive values. I've had to treat them as strings and write a > function to turn them into longs a character at a time ! I couldn't > find a built in function that would do this for me. > > Regards, > > Fuzzy > > http://www.voidspace.org.uk/atlantib...thonutils.html
Michael Foord <fu******@gmail.com> wrote: How do you safely turn an arbitrarily long signed hex string into a long integer ? e.g. -0x55aff8080000
I don't really see what the problem is.
$ python -c 'print int("-0x55aff8080000", 16)'
-94214268911616
Works fine on 2.3.4, no warning. The "0x" is optional,
so if your strings don't contain it from the beginning,
you don't have to insert it.
Best regards
Oliver
--
Oliver Fromme, Konrad-Celtis-Str. 72, 81369 Munich, Germany
``All that we see or seem is just a dream within a dream.''
(E. A. Poe) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Krakatioison |
last post by:
My sites navigation is like this:
http://www.newsbackup.com/index.php?n=000000000040900000
, depending on the variable "n" (which is always a number), it will take me
anywhere on the site......
|
by: Stu Cazzo |
last post by:
I have the following:
String myStringArray;
String myString = "98 99 100";
I want to split up myString and put it into myStringArray.
If I use this:
myStringArray = myString.split(" ");
it...
|
by: John F Dutcher |
last post by:
I use code like the following to retrieve fields from a form:
recd =
recd.append(string.ljust(form.getfirst("lname",' '),15))
recd.append(string.ljust(form.getfirst("fname",' '),15))
etc.,...
|
by: Angus Leeming |
last post by:
Hello,
Could someone explain to me why the Standard conveners chose to typedef
std::string rather than derive it from std::basic_string<char, ...>?
The result of course is that it is...
|
by: Andrew |
last post by:
I have written two classes : a String Class based on the book " C++ in
21 days " and a GenericIpClass listed below :
file GenericStringClass.h
// Generic String class
|
by: zoro |
last post by:
Hi,
I am new to C#, coming from Delphi. In Delphi, I am using a 3rd party
string handling library that includes some very useful string
functions, in particular I'm interested in BEFORE (return...
|
by: Badass Scotsman |
last post by:
Hello,
Using VB and ASP,NET I would like to be able to search a STRING for a
smaller STRING within, based on the characters which appear before and
after.
For example:
String1 = " That was...
|
by: morleyc |
last post by:
Hi, i would like to remove a number of characters from my string (\t
\r \n which are throughout the string), i know regex can do this but i
have no idea how. Any pointers much appreciated.
Chris
|
by: ramu |
last post by:
Hi,
Suppose I have a string like this:
"I have a string \"and a inner string\\\" I want to remove space in
this string but not in the inner string"
In the above string I have to remove...
|
by: drjay1627 |
last post by:
hello,
This is my 1st post here!
*welcome drjay*
Thanks! I look answering questions and getting answers to other!
Now that we got that out of the way. I'm trying to read in a string and...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
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...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
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...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |