Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Converting strings to int

Reply
Thread Tools

Converting strings to int

 
 
allthecoolkidshaveone@gmail.com
Guest
Posts: n/a
 
      05-24-2007
I want to convert a string representation of a number ("1234") to an
int, with overflow and underflow checking. Essentially, I'm looking
for a strtol() that converts int instead of long. The problem with
strtol() is that a number that fits into a long might be too big for
an int. sscanf() doesn't seem to do the over/underflow checking.
atoi(), of course, doesn't do any checking. I've long thought it odd
that there aren't strtoi() and friends for int and short types in the
standard.

Any suggestions?

 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      05-24-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> I want to convert a string representation of a number ("1234") to an
> int, with overflow and underflow checking. Essentially, I'm looking
> for a strtol() that converts int instead of long. The problem with
> strtol() is that a number that fits into a long might be too big for
> an int. sscanf() doesn't seem to do the over/underflow checking.
> atoi(), of course, doesn't do any checking. I've long thought it odd
> that there aren't strtoi() and friends for int and short types in the
> standard.
>
> Any suggestions?
>

Use strtol() and check the result to see if it fits in an int.

--
Ian Collins.
 
Reply With Quote
 
 
 
 
allthecoolkidshaveone@gmail.com
Guest
Posts: n/a
 
      05-24-2007
On May 24, 12:22 am, (E-Mail Removed) wrote:
> I want to convert a string representation of a number ("1234") to an
> int, with overflow and underflow checking.


Of course, 30 seconds later, I think to myself "Why not convert to a
long and see if it's between INT_MIN and INT_MAX and if so return
that value casted to an int?"



 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      05-24-2007
(E-Mail Removed) wrote:
> I want to convert a string representation of a number ("1234") to an
> int, with overflow and underflow checking. Essentially, I'm looking
> for a strtol() that converts int instead of long. The problem with
> strtol() is that a number that fits into a long might be too big for
> an int. sscanf() doesn't seem to do the over/underflow checking.
> atoi(), of course, doesn't do any checking. I've long thought it odd
> that there aren't strtoi() and friends for int and short types in the
> standard.


Check the long value against INT_MAX and INT_MIN.
Then you have the value (if the conversion to long worked), even if out
of range for an int, and the error checking you want.
 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      05-24-2007
(E-Mail Removed) said:

> On May 24, 12:22 am, (E-Mail Removed) wrote:
>> I want to convert a string representation of a number ("1234") to an
>> int, with overflow and underflow checking.

>
> Of course, 30 seconds later, I think to myself "Why not convert to a
> long and see if it's between INT_MIN and INT_MAX and if so return
> that value casted to an int?"


If it is between those values, you don't need a cast. And if it isn't, a
cast won't do any good anyway.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      05-24-2007
Richard Heathfield <(E-Mail Removed)> writes:
> (E-Mail Removed) said:
>> On May 24, 12:22 am, (E-Mail Removed) wrote:
>>> I want to convert a string representation of a number ("1234") to an
>>> int, with overflow and underflow checking.

>>
>> Of course, 30 seconds later, I think to myself "Why not convert to a
>> long and see if it's between INT_MIN and INT_MAX and if so return
>> that value casted to an int?"

>
> If it is between those values, you don't need a cast. And if it isn't, a
> cast won't do any good anyway.


But if you want to store the result in an int, you *will* need a
conversion. This conversion will be done implicitly when you assign
the value.

A lot of people aren't aware that the term "cast" refers *only* to the
explicit cast operator, using a type name in parentheses.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <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."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      05-24-2007
Keith Thompson said:

> Richard Heathfield <(E-Mail Removed)> writes:
>> (E-Mail Removed) said:
>>>
>>> [...] "Why not convert to a
>>> long and see if it's between INT_MIN and INT_MAX and if so return
>>> that value casted to an int?"

>>
>> If it is between those values, you don't need a cast. And if it
>> isn't, a cast won't do any good anyway.

>
> But if you want to store the result in an int, you *will* need a
> conversion. This conversion will be done implicitly when you assign
> the value.


Or you can simply return it:

int foo(const char *s)
{
long int x = whatever(s);
validate_or_die(x);
return x;
}

>
> A lot of people aren't aware that the term "cast" refers *only* to the
> explicit cast operator, using a type name in parentheses.


Yes, sure, but do we really need to include a full chapter of
explanation in every single reply we post?

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      05-24-2007
Richard Heathfield <(E-Mail Removed)> writes:
> Keith Thompson said:
>> Richard Heathfield <(E-Mail Removed)> writes:
>>> (E-Mail Removed) said:
>>>> [...] "Why not convert to a
>>>> long and see if it's between INT_MIN and INT_MAX and if so return
>>>> that value casted to an int?"
>>>
>>> If it is between those values, you don't need a cast. And if it
>>> isn't, a cast won't do any good anyway.

>>
>> But if you want to store the result in an int, you *will* need a
>> conversion. This conversion will be done implicitly when you assign
>> the value.

[...]
>> A lot of people aren't aware that the term "cast" refers *only* to the
>> explicit cast operator, using a type name in parentheses.

>
> Yes, sure, but do we really need to include a full chapter of
> explanation in every single reply we post?


No, but it seemed reasonable in this case. The OP incorrectly thought
he needed a cast; the common confusion between "cast" and "conversion"
is a likely explanation of his confusion.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <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."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
David Tiktin
Guest
Posts: n/a
 
      05-24-2007
On 24 May 2007, (E-Mail Removed) wrote:

> I want to convert a string representation of a number ("1234") to

an
> int, with overflow and underflow checking. Essentially, I'm looking
> for a strtol() that converts int instead of long. The problem with
> strtol() is that a number that fits into a long might be too big

for
> an int. sscanf() doesn't seem to do the over/underflow checking.
> atoi(), of course, doesn't do any checking. I've long thought it

odd
> that there aren't strtoi() and friends for int and short types in

the
> standard.
>
> Any suggestions?


It's actually harder than it looks to use strtol() properly. Here's
the guts a wrapper function I wrote for ints. The wrapper returns 1
if the conversion was OK, 0 otherwise and outputs the value through a
parameter:

Code:
char *  end = NULL;
long    value;

errno = 0;
value = strtol(str, &end, base);

/*
     end == NULL if the base is invalid.
     end == str  if no conversion was done.
    *end == '\0' or *end is whitespace if the number was
            whitespace delimited (a reasonable assumption).
    errno is 0 if no overflow or underflow occurred.
*/
if (end != NULL && end != str && errno == 0 &&
     (*end == '\0' || isspace(*end)))
{
    if (INT_MIN <= value && value <= INT_MAX)
    {
        *integer = (int) value;

        return 1;
    }
}

return 0;
I wonder if anyone would care to comment on whether this method is
adequate.

Dave

--
D.a.v.i.d T.i.k.t.i.n
t.i.k.t.i.n [at] a.d.v.a.n.c.e.d.r.e.l.a.y [dot] c.o.m
 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      05-24-2007
David Tiktin said:

<snip>

> if (end != NULL && end != str && errno == 0 &&
> (*end == '\0' || isspace(*end)))


<snip>

> I wonder if anyone would care to comment on whether this method is
> adequate.


A cursory glance reveals to me only that you are perhaps a little
optimistic in passing *end to isspace(), which requires that its
parameter be representable as an unsigned char. If, for example, *end
were -1, this would not qualify, and the behaviour would be undefined.

This is one of those very rare and bizarre cases where it is actually a
*good* idea to use a cast - isspace((unsigned char)*end) - and the
normal promotion rules will of course take care of the conversion to
int for you.


--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
why is int a[0] not allowed, but int* a = new int[0] is? haijin.biz@gmail.com C++ 9 04-17-2007 09:01 AM
Difference between int i, j; and int i; int j; arun C Programming 8 07-31-2006 05:11 AM
int a[10]; int* p=(int*)((&a)+1); But why p isn't equal to ((&a)+1)? aling C++ 8 10-20-2005 02:42 PM
int main(int argc, char *argv[] ) vs int main(int argc, char **argv ) Hal Styli C Programming 14 01-20-2004 10:00 PM
dirty stuff: f(int,int) cast to f(struct{int,int}) Schnoffos C Programming 2 06-27-2003 03:13 AM



Advertisments