String To Int Conversion

What is Type Conversion?

In the computer science type conversion (type casting, and type coercion) means transforming values of one type to another type of value, for example converting an string to int value (or  int to string). Not only primitive data type can be converted but also compound data types, for example list to string, array to string (SEE MORE).

It is good to know that type conversions can take advantage of certain features of type hierarchies and data representations.

There are explicit and implicit type conversion. Explicit casting is defined by programmer in the code through: specific language construction; functions that take the value of one type and returns a value of another type. Implicit casting is performed by compiler (or interpreter) according to the rules described in the standard language. The standard of the most of languages forbid implicit conversions.

Types of conversions

Implicit Type Conversion

Implicit type conversion (coercion), in this case, the compiler automatically converts the type and programmer doesn’t play any role.

Although d, l and i belong to different data types, they will be automatically converted to equal data types each time a comparison or assignment is executed.

This behavior should be used with caution, because unintended consequences may arise. Data may be lost, for example:

  • float to int conversion causes truncation, in other words, removal of the fractional part.
  • double to float causes rounding of digit.
  • long long to int causes dropping of excess higher order bits as a result the data can be totally different.

Example program in C++:

Output:Implicit Type Conversion (coercion)

Type promotion

Type promotion is a special case of implicit type conversion; the compiler automatically expands the binary representation of objects of integer or floating-point types. Promotions are commonly used with types smaller than the native type of the target platform’s arithmetic logic unit (ALU), before arithmetic and logical operations, to make such operations possible, or more efficient if the ALU can work with more than one type.


Type Promotion

Type promotions never lose precision or modify the value.

Explicit type conversion

Explicit type conversion is a type conversion which is explicitly defined within a program, this is also called typecasting (Type Casting).

Example of type casting:

Output:Explicit Type Conversion

As well as Implicit Type Conversion in this case data may be lost.

How to convert String to int?

To convert from string to int we must know the ASCII (American Standard Code for Information Interchange) code of each number, so below is a list of ASCII characters and their decimal value.

In order to get the decimal value of each string element, we have to subtract it with decimal value of character ‘0’ (48). For example, we have the char ‘5’ which is equals  to 53, so 53 – 48 = 5.

Converting String to int: Pseudo code

  1. Start at 0
  2. If the first character is ‘-‘
    Set the negative flag (isNegative = TRUE)
  3. Else if the first character is not digit, then throw exception
  4. Else for each character
    Multiply the number by 10
    Add the (digit – 48) to number
  5. If the flag is negative
    Negate the number
  6. Return the number

Let’s make this clear with the help of an example.

String To Int Algorithm Table

Conversion in different programming languages


To convert string to int in C there is the atoi() function. Due to this function we can convert the string argument str to an integer ( int). The syntax of this function is:

Here, str is of type pointer to a character and this is the string representation of an integral number. As a result this function returns an integer value. Either, if no valid conversion could be performed, it returns zero. The atoi() function is present in the stdlib.h header file. This header file contains all the type casting functions used in the C language. The following example shows the usage of atoi() function.

Output:String to int C atoi function

In addition to learn more about conversion from String to int in C language, please visit this page.


In C++11 there are new convert functions from string to int. So instead of atoi() you can use std::stoi() where str is your number as std::string. The syntax of these functions are:

str is an std::string object with the representation of an integral number; idx represents address of an integer to store the number of characters processed and base – the number base. Finally here is an example program.

Output:String to int C++ stoi function

If no conversion could be performed, an invalid_argument exception is thrown.

Older compilers may not support it because this function (stoi()) is a feature introduced by the latest revision of the C++ standard (2011).

More about string to int conversion in C++ you can learn here.


In C# you can convert string to int by using methods in the Convert class or by using the TryParse method found on the Int32 class.

You can use Parse or TryParse methods in the  Int32 class. If the string is not in a valid format, Parse throws an exception whereas TryParse returns fasle. Also the ToInt32() method in the Convert class throws FormatException and OverflowException if the string is not valid.


Output:String To int C# Parse(), TryParse() and toint32() methods

More about string to int conversion in C# you can learn here.


In Java, you can convert string to int by using methods in the Integer class. These methods are parseInt()  and valueOf(). The syntax of these methods are:

Where s is the String containing the integer representation to be parsed and radix – the radix to be used while parsing s.

Example of using these methods:


These methods throw NumberFormatException if the string cannot be parsed as an integer.

Read more on page.


To convert string to int in Python we can use  int() function. The syntax:

The parameter x – number or string. base is optional parametter a number (from 2 to 36) that represents the numeral system to be used.



More here.


intval() get the integer value of a variable.

Function syntax:

This function returns the integer value of $var, using the specified $base for the conversion. The default is base 10. intval() should not be used on objects, as doing so will emit an E_NOTICE level error and return 1.



For more details visit this page.


The parseInt() function parses a string argument and returns an integer.


The string parametter is value to parse. If the string argument is not a string, then it is converted to a string. Also, leading white spaces in the string argument are ignored. radix specifies the base of the number whose string representation is contained in the string. We can use it only when necessary. The base argument can be any integer from 2 to 36, by default it is 10.


Output:String To Int JavaScript parseInt() function

In addition, you can learn more about string to int conversion in JavaScript (JS) visit this page.

2 thoughts on “String To Int Conversion”

  1. good job, add a new categories as “string to float” and “string to double”. I think you should add an example for Objective C. Also, many people are looking for how to convert string into date and time.

    1. Hello Andrew, thank for your comment. New categories will be added soon. I plan to add string to time and date, and object oriented programming language – Objective C.

Leave a Reply

Your email address will not be published. Required fields are marked *