Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equivalent of atoi for unsigned integers

Tags:

I'm doing two operations involving atoi and I'm wondering how I can do this with unsigned integers because atoi seems to convert these to signed causing a wraparound integer overflow. I want to work with 32bit unsigned integers but atoi is limiting me effectively to 31bit unsigned.

 if (multiplication_is_safe(atoi(argv[1]),atoi(argv[3])))     {      printf("%s * %s = %u \n", argv[1], argv[3], atoi(argv[1]) * atoi(argv[3]));       return 0;     }  else 
like image 755
John Avatar asked Dec 02 '14 22:12

John


People also ask

How do you convert a signed integer to an unsigned integer?

To convert a signed integer to an unsigned integer, or to convert an unsigned integer to a signed integer you need only use a cast. For example: int a = 6; unsigned int b; int c; b = (unsigned int)a; c = (int)b; Actually in many cases you can dispense with the cast.

Is atoi deprecated?

atoi is not deprecated, your source is incorrect. Nothing in the current C standard ISO 9899:2011 indicates this (see for example chapter 6.11 future language directions), nor anything in earlier standards. As per the C standard, atoi is equivalent to strtol as follows, C11 7.22.

Does atoi work for long?

Description. The atol() function converts a character string to a long value. The atoll() function converts a character string to a long long value. The input string is a sequence of characters that can be interpreted as a numeric value of the specified return type.

What happens if atoi fails in C?

Return Value The atoi() function returns an int value that is produced by interpreting the input characters as a number. The return value is 0 if the function cannot convert the input to a value of that type. The return value is undefined in the case of an overflow.


2 Answers

The simple answer is to use strtoul() instead.

The longer answer is that even if all you needed was signed 32 bit integers or were happy with 31 bits for unsigned, the atoi() function is a poor fit for what you appear to be doing.

As you have already noted, the atoi() function converts a string to an integer. A normal, signed integer. However, what atoi() doesn't do is error handling. What atoi()'s specification says is "If the value cannot be represented, the behavior is undefined."

The strto*() family of functions all clearly specify how errors are handled, so you should in all cases replace atoi() with calls to strtol() (convert string to long), and in this case since you want to handle unsigned integers, you should use strtoul() (convert string to unsigned long).

Also note that if you want to handle larger numbers, there are the strtoll() and strtoull() functions, to convert your string to a long long or an unsigned long long. (And if you just want to handle the largest possible integral values without bothering with all that stuff in between, there's strtoimax() and strtoumax(), that return values of type intmax_t or uintmax_t respectively.)

POSIX Documentation:

  • atoi()
  • strtol()
  • strtoll()
  • strtoul()
  • strtoull()
  • strtoimax()
  • strtoumax()
like image 124
This isn't my real name Avatar answered Sep 17 '22 12:09

This isn't my real name


Depending on your platform, strtoul is probably what you want:

The strtoul() function converts the initial part of the string in nptr to an unsigned long int value according to the given base, which must be between 2 and 36 inclusive, or be the special value 0.

like image 41
The Archetypal Paul Avatar answered Sep 18 '22 12:09

The Archetypal Paul