Re: convert string to size_t
- From: Ben Bacarisse <ben.usenet@xxxxxxxxx>
- Date: Sun, 23 Nov 2008 23:19:21 +0000
rtillmore@xxxxxxxxx writes:
On Nov 23, 9:30 pm, Peter Nilsson <ai...@xxxxxxxxxxx> wrote:
rtillm...@xxxxxxxxx wrote:
Hi,
There is a nice function called atoi that converts
strings to ints.
Better is strtol().
Is there a way to convert a string to size_t?
There is strtol and strtoul in C90, and additionally
strtoimax and strtoumax in C99. It's often better
to use the signed conversions since the unsigned
conversion don't capture errors in values that would
be outside the range of the result unsigned type.
--
Peter
It is best not to quote sigs.
Thanks for the information. My data will never be less
than zero so unsigned is good enough.
OK, but I worry that you may not have picked up on Peter Nilsson's
warning. Your (valid) data may never be less that zero, but you can't
easily check that this is the case using the unsigned strtou*
functions. For example, strtoul("-1", NULL, 0) returns ULONG_MAX
without any indication of an error. strtol will return -1 so you can
report an invalid input.
If by "my data will never be less than zero" you meant that you don't
need to check for negative data, then all is well, but I thought it
worth checking.
--
Ben.
.
- Follow-Ups:
- Re: convert string to size_t
- From: Andrey Tarasevich
- Re: convert string to size_t
- References:
- convert string to size_t
- From: rtillmore
- Re: convert string to size_t
- From: Peter Nilsson
- Re: convert string to size_t
- From: rtillmore
- convert string to size_t
- Prev by Date: Re: long offset
- Next by Date: Unable to bind a 2nd time if recv receives all bytes sent
- Previous by thread: Re: convert string to size_t
- Next by thread: Re: convert string to size_t
- Index(es):
Relevant Pages
|