C++

C++ Itoa

There are several different ways to convert a string into an integer i.e., writing a customized program or using the built-in functions. C++ programing language provides multiple types of built-in functions to convert an integer into a string. One such function is an itoa function. This tutorial will help you understand the working of the itoa function in the c++ programming language. The examples will guide you on how to use the itoa function in your program and convert an integer to a string.

What is an Itoa Function in C++ Language?

The itoa is a c++ function available in the stdlib.h library. The itoa function is used to convert an integer number into a string. It converts an integer into a null terminated string using the specified base and stores the result into an array. Positive numbers are converted into a positive string if the given base is 10. If the number is negative, the resulting string has a preceding negative sign (-). In the case of any other base, both positive and negative numbers converted to a string without any preceding sign, are always considered unsigned. The resultant value is stored in an array that should be long enough to hold any possible length value.

Syntax of Itoa Function

The syntax of the itoa function is given below, have a look:

The itoa function takes three parameters: input, output, and base. It returns the converted value in the form of a string. The ‘value’ parameter holds the value that needs to be converted. The ‘str’ parameter is an array that will hold the resultant value, returned by the itoa function. The ‘base’ parameter is used to define the base in which the conversion of the ‘value’ needs to be performed. Let us understand the working of the itoa function with examples.

Example 1:

In this example, we will take an input number from the user and convert it into a string using the itoa function. First, create a new file in dev c++ and start writing your code. Then, save the file in your local drive before execution. If you do not do so, the compiler will ask you to save the file when you attempt to execute the code without saving the file. Now, let us understand the code given below:

#include <stdio.h>
#include <stdlib.h>
#include<iostream>
using namespace std;
int main ()
{
  int a;
  char b [50];
  cout<>a;
  itoa (a,b,10);
  cout<<a<<" converted to decimal = "<<b<<endl;
  itoa (a,b,16);
  cout<<a<<" converted to hexadecimal = "<<b<<endl;
  itoa (a,b,2);
  cout<<a<<" converted to binary = "<<b;
    return 0;
}

At first, three header files: stdio.h, stdlib.h, and iostream, are included along with the ‘using namespace std’ statement. The iostream and using namespace std header files are included in the program so the input and output utilities of the c++ programing language can be used in the program. The stdio.h header file is used to include the variable types and several macrons. And finally, the stdlib.h header file is a general-purpose standard library used to include functions like conversions, process control, memory allocation, etc.

The program or lines of code are written in the main() function. In the first line of the main program, a variable ‘a’ of type int is defined to get input from the user. After that, an array of size 50 and type char is defined to store the output of the itoa function.

The cout statement is used to ask the user to provide a number that needs to be converted. Using the cin statement, the input number is taken from the user.

The input ‘a’, output array ‘b’, and base ‘10’ are passed to the itoa function itoa(a, b, 10). The base 10 defines the conversion that needs to be done in decimal format. Using the cout statement, the original number along with its converted string is printed. The same statements are used to convert the input number into hexadecimal and binary format just by changing the base. Base 2 represents the binary number and base 16 represents the hexadecimal conversion.

The return 0 statement is used to indicate that the program is executed successfully and will return nothing in that case.

When you save the file after writing the whole code, press f11 from the function keys on the keyboard or click the execution icon provided on the taskbar of dev c++. See the output below:

The compiler will prompt the user to enter a number. When you enter a number on the terminal from the keyboard, the following result will be generated:

Example 2:

Now, let us move to example 2 and explore some more workings of the itoa function in c++. In the previous example, we defined a static size of the buffer. Here, we are going to define the dynamic size of the buffer so that whatever number the user will enter, the buffer is capable of holding the output. We will be using the code from example 1 and will only change the buffer size from static to dynamic. Here is the main code:

#include <stdio.h>
#include <stdlib.h>
#include<iostream>
using namespace std;
int main ()
{
  int a;
  char b [sizeof(int)*8+1];
  cout<>a;
  itoa (a,b,10);
  cout<<a<<" converted to decimal = "<<b<<endl;
  itoa (a,b,16);
  cout<<a<<" converted to hexadecimal = "<<b<<endl;
  itoa (a,b,2);
  cout<<a<<" converted to binary = "<<b;
    return 0;
}

The ‘sizeof(int)*8+1’ is used to define the dynamic size of the buffer that will hold the output in the end. The sizeof() is a built-in function provided by the standard library of c++ that is used to get the size of the specified element. In this case, the sizeof() function is checking the size of the input int number. The found size of the input is multiplied by 8 and 1 will be added to it. This will always result in a bigger buffer size which will always be enough for holding the output. The results won’t be affected in any way that can be seen. View the outcome below:

The compiler prompts the user to provide a number. Now, you can enter even a 10-digit long number and the buffer can hold the result without overflowing, see the result below:

Note that the binary conversion is 31 bits long and it is correctly presented by the output array.

Example 3:

Let us examine the behavior of the itoa function when we enter an invalid or negative number. By using the same code from the above example, we will test the itoa function with a negative integer number. Here is the same code:

#include <stdio.h>
#include <stdlib.h>
#include<iostream>
using namespace std;
int main ()
{
  int a;
  char b [sizeof(int)*8+1];
  cout<>a;
  itoa (a,b,10);
  cout<<a<<" converted to decimal = "<<b<<endl;
  itoa (a,b,16);
  cout<<a<<" converted to hexadecimal = "<<b<<endl;
  itoa (a,b,2);
  cout<<a<<" converted to binary = "<<b;
    return 0;
}

See the output below:

Now, let us enter a negative number:

As discussed above, only the base 10 conversion will have a preceding minus sign (-) with converted output and all other base numbers will be unsigned.

Conclusion

This is an overview of the itoa function. The itoa function takes three arguments, input number, output array, and base number in which the conversion needs to be performed. It takes an integer number, converts it into a string specified by the base, and returns the converted string.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.