473,396 Members | 2,024 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.

Converting large strings to numbers

Without using errno, is there a portable way to detect if a string
number is not within the range 0 to 4294967295.

Currently I am using strtoul. I can check the string for "-" as the
first character before calling strtoul, so I can figure out if the
value is less than 0.

My issue arises when the number is greater than 4294967295 on platforms
where ULONG_MAX is 4294967295; in this case strtoul will return
ULONG_MAX and set errno to ERANGE. However, some platforms I work on do
not support errno. So there is no way for me to know that the value was
greater than 4294967295.

Is there another way to do this ?

TIA,
rouble

Nov 14 '05 #1
8 1940
In article <11*********************@l41g2000cwc.googlegroups. com>,
<ro****@gmail.com> wrote:
Without using errno, is there a portable way to detect if a string
number is not within the range 0 to 4294967295. Currently I am using strtoul. I can check the string for "-" as the
first character before calling strtoul, so I can figure out if the
value is less than 0. My issue arises when the number is greater than 4294967295 on platforms
where ULONG_MAX is 4294967295; in this case strtoul will return
ULONG_MAX and set errno to ERANGE. However, some platforms I work on do
not support errno. So there is no way for me to know that the value was
greater than 4294967295.


Stringify ULONG_MAX and do a strcmp() of the source number against
that value. If the source number is all digits then strcmp() of the
decimal representations is equivilent to a numeric comparison.
Even in odd character sets, because of the C standard's specifi
rules about the representation of the digits.
--
"Mathematics? I speak it like a native." -- Spike Milligan
Nov 14 '05 #2

rou...@gmail.com wrote:
Without using errno, is there a portable way to detect if a string
number is not within the range 0 to 4294967295.

Currently I am using strtoul. I can check the string for "-" as the
first character before calling strtoul, so I can figure out if the
value is less than 0.

My issue arises when the number is greater than 4294967295 on platforms where ULONG_MAX is 4294967295; in this case strtoul will return
ULONG_MAX and set errno to ERANGE. However, some platforms I work on do not support errno. So there is no way for me to know that the value was greater than 4294967295.

Is there another way to do this ?

TIA,
rouble


Well, if strtoul returns ULONG_MAX, you could compare the string being
converted to "4294967295". If it is NOT the same, you have overflowed.

-David

Nov 14 '05 #3


ro****@gmail.com wrote:
Without using errno, is there a portable way to detect if a string
number is not within the range 0 to 4294967295.

Currently I am using strtoul. I can check the string for "-" as the
first character before calling strtoul, so I can figure out if the
value is less than 0.

My issue arises when the number is greater than 4294967295 on platforms
where ULONG_MAX is 4294967295; in this case strtoul will return
ULONG_MAX and set errno to ERANGE. However, some platforms I work on do
not support errno. So there is no way for me to know that the value was
greater than 4294967295.

Is there another way to do this ?


A platform that doesn't "support errno" is a non-conforming
implementation of C, so you're in uncertain territory at best.
One possibility, of course, is to write your own replacement; if
the implementation is already known to be non-conforming and
hence suspect, this may be the safest course.

If you can at least trust strtoul() to return ULONG_MAX for
an out-of-range number, you could conclude that any other value
is legitimate. When ULONG_MAX shows up, you could inspect the
string itself to see whether it was in fact "4294967295" (or
"037777777777" or "0xFFFFFFFF" or ... it'll be a little more
trouble than a mere strcmp(), and the tests will depend on the
value of strtoul()'s third argument). Or (if circumstances
permit) you could just treat ULONG_MAX as an error, sacrificing
the possibility of actually handling that single large number.

--
Er*********@sun.com

Nov 14 '05 #4
ro****@gmail.com wrote:

Without using errno, is there a portable way to detect if a string
number is not within the range 0 to 4294967295.

Currently I am using strtoul. I can check the string for "-" as the
first character before calling strtoul, so I can figure out if the
value is less than 0.

My issue arises when the number is greater than 4294967295 on
platforms where ULONG_MAX is 4294967295; in this case strtoul will
return ULONG_MAX and set errno to ERANGE. However, some platforms I
work on do not support errno. So there is no way for me to know that
the value was greater than 4294967295.

Is there another way to do this ?


You obviously have a non-conformant implementation. However, you
can do the fundamental conversions yourself. For input from a
stream see txtio.zip, and if you need string input make the
appropriate changes yourself. See:

<http://cbfalconer.home.att.net/download/>

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson

Nov 14 '05 #5
ro****@gmail.com wrote:

Without using errno, is there a portable way to detect if a string
number is not within the range 0 to 4294967295.

Currently I am using strtoul. I can check the string for "-" as the
first character before calling strtoul, so I can figure out if the
value is less than 0.

My issue arises when the number
is greater than 4294967295 on platforms
where ULONG_MAX is 4294967295; in this case strtoul will return
ULONG_MAX and set errno to ERANGE.
However, some platforms I work on do
not support errno.
So there is no way for me to know that the value was
greater than 4294967295.

Is there another way to do this ?


Use strcmp instead of strtoul.

/* BEGIN new.c */

#include <stdio.h>
#include <limits.h>
#include <ctype.h>

#define str(s) # s
#define xstr(s) str(s)

#define RANGE_1 "4294967295"

int all_digits(char *s);

int main(void)
{
char *range = RANGE_1;

/* char *range = xstr(ULONG_MAX); */
/* char *range = RANGE_1 */

char *strings[] = {
"0","","-1","4294967296","4294967296000",
"4294967294","4294967295", NULL
};
char **s = strings;

printf("\nRange is %s.\n\n", range);
while (*s != NULL) {
if (**s != '\0' && all_digits(*s)
&& 0 >= strcmp(*s, range)) {
printf("%s is in range.\n", *s);
} else {
printf("%s is out of range.\n", *s);
}
++s;
}
return 0;
}

int all_digits(char *s)
{
while(isdigit(*s)) {
++s;
}
return *s == '\0';
}

/* END new.c */

--
pete
Nov 14 '05 #6
pete wrote:
/* char *range = xstr(ULONG_MAX); */


That one doesn't work right.

It's "0xffffffffUL" on my machine.

--
pete
Nov 14 '05 #7
On Thu, 28 Apr 2005 16:42:00 +0000, Walter Roberson wrote:
In article <11*********************@l41g2000cwc.googlegroups. com>,
<ro****@gmail.com> wrote:
Without using errno, is there a portable way to detect if a string
number is not within the range 0 to 4294967295.
Currently I am using strtoul. I can check the string for "-" as the
first character before calling strtoul, so I can figure out if the
value is less than 0.

My issue arises when the number is greater than 4294967295 on platforms
where ULONG_MAX is 4294967295; in this case strtoul will return
ULONG_MAX and set errno to ERANGE. However, some platforms I work on do
not support errno. So there is no way for me to know that the value was
greater than 4294967295.


Stringify ULONG_MAX


Ok as long as you mean convert the value to a string with, say, sprintf()
and not by preprocessor tricks on the ULONG_MAX macro itself.
and do a strcmp() of the source number against
that value. If the source number is all digits then strcmp() of the
decimal representations is equivilent to a numeric comparison.
Unless there are leading zeros.
Even in
odd character sets, because of the C standard's specifi rules about the
representation of the digits.


It would still work even if the digits weren't contiguous in the character
set.

Lawrence

Nov 14 '05 #8
"David Resnick" <ln********@gmail.com> writes:
rou...@gmail.com wrote:
Without using errno, is there a portable way to detect if a string
number is not within the range 0 to 4294967295.

Currently I am using strtoul. I can check the string for "-" as the
first character before calling strtoul, so I can figure out if the
value is less than 0.

My issue arises when the number is greater than 4294967295 on
platforms where ULONG_MAX is 4294967295; in this case strtoul will
return ULONG_MAX and set errno to ERANGE. However, some platforms I
work on do not support errno. So there is no way for me to know
that the value was greater than 4294967295.

Is there another way to do this ?


Well, if strtoul returns ULONG_MAX, you could compare the string being
converted to "4294967295". If it is NOT the same, you have overflowed.


Unless the string is "+4294967295", or "04294967295", or
"+0004294967295". That's if you use a "base" argument of 10; if you
set "base" to 0, a leading '0' implies octal, and you also have to
worry about "037777777777", "00037777777777", "0xffffffff",
"0X000FfFfFfFf", and so forth.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #9

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

Similar topics

5
by: nickisme | last post by:
Hi - sorry for the possibly stupid question, but I'm still a wee starter on c++... Just wondering if there's a quick way to convert data into binary strings... To explain, I'm trying to convert...
2
by: Mariusz Sakowski | last post by:
I'm writing class which will be able to store large numbers (my ambition is to make it able to operand on thousands of bits) and perform various operations on it (similiar to those available with...
30
by: zexpe | last post by:
I have an extremely cpu/data intensive piece of code that makes heavy use of the following function: void convertToDouble(const std::string& in, double& out) { out = atof(in.c_str()); } I...
22
by: Frinton | last post by:
Hi, I am trying to do some calculations on large numbers (ie 7,768,489,957,892,578,474,792,094 / 12,280) and no matter what I do it doesn't get it quite right. Its always somewhere between 10...
3
by: CFonville | last post by:
I was wondering if there is any way to store large numbers in a variable? With this simple script: var bigpi = 1234567890123456789012345678901234567890123456789012345678901234567890;...
116
by: Dilip | last post by:
Recently in our code, I ran into a situation where were stuffing a float inside a double. The precision was extended automatically because of that. To make a long story short, this caused...
11
by: rshepard | last post by:
I start with a list of tuples retrieved from a database table. These tuples are extracted and put into individual lists. So I have lists that look like this: . When I concatenate lists, I end up...
2
by: CoreyWhite | last post by:
Problem: You have numbers in string format, but you need to convert them to a numeric type, such as an int or float. Solution: You can do this with the standard library functions. The...
21
by: py_genetic | last post by:
Hello, I'm importing large text files of data using csv. I would like to add some more auto sensing abilities. I'm considing sampling the data file and doing some fuzzy logic scoring on the...
0
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
0
BarryA
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...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.