0

Is there a stricter strtoull() in any ubiquitous C library?

 1 year ago
source link: https://softwareengineering.stackexchange.com/questions/251508/is-there-a-stricter-strtoull-in-any-ubiquitous-c-library
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Is there a stricter strtoull() in any ubiquitous C library?

Software Engineering Stack Exchange is a question and answer site for professionals, academics, and students working within the systems development life cycle. It only takes a minute to sign up.

Sign up to join this community

Anybody can ask a question
Anybody can answer
The best answers are voted up and rise to the top
Asked 8 years, 3 months ago
Viewed 806 times

I want a function that will interpret a string as a strictly unsigned integer, failing when the string overflows, represents a negative number, or does not represent a number.

strtoull() does set errno to ERANGE on overflow or EINVAL for a string that does not begin with a number, but accepts negative numbers as valid.

strtoumax() is the same as strtoull()

atoll() and strtonum() are ruled out because they are strictly for signed integers.

sscanf() sets errno to ERANGE on overflow and stops processing the string if it does not begin with a number (reflected in sscanf's return value), but also accepts negative numbers as valid.

Is there another option that I'm missing?

asked Jul 29, 2014 at 1:22

2 Answers

There are no out-of-the-box solutions that fit your requirements, but it is easy enough to write such a function yourself, using strtoull or strtoll as basis.

For negative numbers, either you can check for the minus sign beforehand, or you can convert to a signed number first and report an error for negative values.
For numbers followed by other characters, you can check that the endp pointer that you pass to strto(u)ll points to the end of the string.

answered Jul 29, 2014 at 8:01

You are describing two separate problems. One is that the string should be in a specific form, namely all digits (but you didn't specify whether to permit leading and trailing spaces). The second is that the range should not overflow. There is no readily available function that provides exactly this combination.

The first requirement is best satisfied by testing (or parsing) the string before decoding it. In most languages I would use a regular expression. In C++ you can use library functions. In C you write loops and test characters.

The second requirement is actually quite tricky. You can include overflow testing in your decode function but testing against MAXUINT or something similar needs some careful coding. A simple solution is to decode as a double and then test the range before casting to uint.

In summary, when you have requirements as specific as this you normally finish up writing your own functions, perhaps basing it on available "free" source code. Over the 30 years I've been writing C code I've probably written this function or similar about 5 or 6 times. It gets easier with practice!

answered Jul 29, 2014 at 11:58

Your Answer

Sign up or log in

Sign up using Google
Sign up using Facebook
Sign up using Email and Password

Post as a guest

Name
Email

Required, but never shown

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK