Usually this is done like this:

  • init result with 0
  • for each character in string do this
    • result = result * 10
    • get the digit from the character ('0' is 48 ASCII (or 0x30), so just subtract that from the character ASCII code to get the digit)
    • add the digit to the result
  • return result

Edit: This works for any base if you replace 10 with the correct base and adjust the obtaining of the digit from the corresponding character (should work as is for bases lower than 10, but would need a little adjusting for higher bases - like hexadecimal - since letters are separated from numbers by 7 characters).

Edit 2: Char to digit value conversion: characters '0' to '9' have ASCII values 48 to 57 (0x30 to 0x39 in hexa), so in order to convert a character to its digit value a simple subtraction is needed. Usually it's done like this (where ord is the function that gives the ASCII code of the character):

digit = ord(char) - ord('0')

For higher number bases the letters are used as 'digits' (A-F in hexa), but letters start from 65 (0x41 hexa) which means there's a gap that we have to account for:

digit = ord(char) - ord('0')
if digit > 9 then digit -= 7

Example: 'B' is 66, so ord('B') - ord('0') = 18. Since 18 is larger than 9 we subtract 7 and the end result will be 11 - the value of the 'digit' B.

One more thing to note here - this works only for uppercase letters, so the number must be first converted to uppercase.

Answer from rslite on Stack Overflow
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ java โ€บ number_parseint.htm
Java - parseInt() Method
public class Test { public static void main(String args[]) { int x =Integer.parseInt("9"); double c = Double.parseDouble("5"); int b = Integer.parseInt("444",16); System.out.println(x); System.out.println(c); System.out.println(b); } }
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ parseint() in java
parseInt() in Java - Scaler Topics
May 8, 2024 - NullPointerException: It arises when the string s given as argument is null. ... A user-defined example where anyone using this code can put a value of their choice and get the equivalent integer as output.
๐ŸŒ
iO Flood
ioflood.com โ€บ blog โ€บ parseint-java
parseInt() Java Method: From Strings to Integers
February 26, 2024 - These integers can then be used in a variety of ways in your Java programs, from calculations to control structures. This guide will explain how to use the parseInt method in Java, from basic usage to more advanced techniques.
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ java โ€บ lang โ€บ integer_parseint.htm
Java - Integer parseInt() method
package com.tutorialspoint; public ... example shows the usage of Integer parseInt() method to get a Integer object from a string containing a negative decimal number....
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ integer-valueof-vs-integer-parseint-with-examples
Integer.valueOf() vs Integer.parseInt() with Examples - GeeksforGeeks
July 11, 2025 - There are two variants of this method: Syntax: public static int parseInt(String s) throws NumberFormatException ยท public static int parseInt(String s, int radix) throws NumberFormatException ยท Example: Java ยท
๐ŸŒ
H2K Infosys
h2kinfosys.com โ€บ blog โ€บ introduction to parseint in java
Introduction to ParseInt in Java
September 24, 2024 - ParseInt, for instance, can be used to verify the validity of an email address before sending an email to it. A straightforward API that is simple to understand is used for parsing and validation.
๐ŸŒ
Javatpoint
javatpoint.com โ€บ java-integer-parseint-method
Java Integer parseInt() Method - Javatpoint
Java Integer parseInt() Method - The Java parseInt() method is a method of Integer class that belong to java.lang package. The parseInt() method in Java is crucial for converting string representations of numbers into actual integer values. This capability is fundamental in various programming ...
๐ŸŒ
LabEx
labex.io โ€บ tutorials โ€บ java-java-integer-parseint-method-117728
Java parseInt(String, int) | Integer Parsing Tutorial | LabEx
In this lab, we have learned how to use the Java parseInt(String s, int radix) method to parse a string value into a signed decimal integer object relative to a specified radix value.
Find elsewhere
๐ŸŒ
Global Tech Council
globaltechcouncil.org โ€บ home โ€บ what is parseint in java?
What is parseInt in Java? - Global Tech Council
July 5, 2025 - String: This version of the method takes a single string parameter. The string should represent a valid integer value. For example, parseInt(โ€œ123โ€) will return the integer 123.
Top answer
1 of 8
47

Usually this is done like this:

  • init result with 0
  • for each character in string do this
    • result = result * 10
    • get the digit from the character ('0' is 48 ASCII (or 0x30), so just subtract that from the character ASCII code to get the digit)
    • add the digit to the result
  • return result

Edit: This works for any base if you replace 10 with the correct base and adjust the obtaining of the digit from the corresponding character (should work as is for bases lower than 10, but would need a little adjusting for higher bases - like hexadecimal - since letters are separated from numbers by 7 characters).

Edit 2: Char to digit value conversion: characters '0' to '9' have ASCII values 48 to 57 (0x30 to 0x39 in hexa), so in order to convert a character to its digit value a simple subtraction is needed. Usually it's done like this (where ord is the function that gives the ASCII code of the character):

digit = ord(char) - ord('0')

For higher number bases the letters are used as 'digits' (A-F in hexa), but letters start from 65 (0x41 hexa) which means there's a gap that we have to account for:

digit = ord(char) - ord('0')
if digit > 9 then digit -= 7

Example: 'B' is 66, so ord('B') - ord('0') = 18. Since 18 is larger than 9 we subtract 7 and the end result will be 11 - the value of the 'digit' B.

One more thing to note here - this works only for uppercase letters, so the number must be first converted to uppercase.

2 of 8
28

The source code of the Java API is freely available. Here's the parseInt() method. It's rather long because it has to handle a lot of exceptional and corner cases.

public static int parseInt(String s, int radix) throws NumberFormatException {
    if (s == null) {
        throw new NumberFormatException("null");
    }

    if (radix < Character.MIN_RADIX) {
        throw new NumberFormatException("radix " + radix +
            " less than Character.MIN_RADIX");
    }

    if (radix > Character.MAX_RADIX) {
        throw new NumberFormatException("radix " + radix +
            " greater than Character.MAX_RADIX");
    }

    int result = 0;
    boolean negative = false;
    int i = 0, max = s.length();
    int limit;
    int multmin;
    int digit;

    if (max > 0) {
        if (s.charAt(0) == '-') {
            negative = true;
            limit = Integer.MIN_VALUE;
            i++;
        } else {
            limit = -Integer.MAX_VALUE;
        }
        multmin = limit / radix;
        if (i < max) {
            digit = Character.digit(s.charAt(i++), radix);
            if (digit < 0) {
                throw NumberFormatException.forInputString(s);
            } else {
                result = -digit;
            }
        }
        while (i < max) {
            // Accumulating negatively avoids surprises near MAX_VALUE
            digit = Character.digit(s.charAt(i++), radix);
            if (digit < 0) {
                throw NumberFormatException.forInputString(s);
            }
            if (result < multmin) {
                throw NumberFormatException.forInputString(s);
            }
            result *= radix;
            if (result < limit + digit) {
                throw NumberFormatException.forInputString(s);
            }
            result -= digit;
        }
    } else {
        throw NumberFormatException.forInputString(s);
    }
    if (negative) {
        if (i > 1) {
            return result;
        } else { /* Only got "-" */
            throw NumberFormatException.forInputString(s);
        }
    } else {
        return -result;
    }
}
๐ŸŒ
vteams
vteams.com โ€บ blog โ€บ use-of-parseint-java-programming
Learn the Basics & Ins-And-Outs of PARSEint Java Programming
February 23, 2024 - First you have to call the method from the integer calls to convert string to int Java programming, which you want to convert. Then the method will run and return you the desired results, provided everything went well. Here is a sample example. The above-mentioned, integer.parseInt Java example is passing โ€˜123โ€™ as a string and not a number into the method, which is being called in the second line from its integer class.
๐ŸŒ
BeginnersBook
beginnersbook.com โ€บ 2022 โ€บ 10 โ€บ java-integer-parseintmethod
Java Integer parseInt()Method
Here, we have two char sequences and we are parsing the part of the these sequences using parseInt() method of Integer class. public class JavaExample { public static void main(String[] args) { String s = "Hello1001Bye"; String s2 = "LEAF"; //taking 1001 part from string s int i = Integer.parse...
๐ŸŒ
Microsoft Learn
learn.microsoft.com โ€บ en-us โ€บ dotnet โ€บ api โ€บ java.lang.integer.parseint
Integer.ParseInt Method (Java.Lang) | Microsoft Learn
[<Android.Runtime.Register("parseInt", "(Ljava/lang/String;I)I", "")>] static member ParseInt : string * int -> int ... Parses the string argument as a signed integer in the radix specified by the second argument.
๐ŸŒ
Study.com
study.com โ€บ courses โ€บ business courses โ€บ business 104: information systems and computer applications
How to Convert String to Int in Java - ParseInt Method - Lesson | Study.com
January 9, 2023 - This lesson describes how to convert a String variable into an integer value using the ParseInt method in Java. Working code samples are provided. ... Sometimes, we need to work with numeric strings as if they're numbers. For example, a string object could hold an integer value, and we might ...
๐ŸŒ
Quora
quora.com โ€บ How-does-Integer-parseInt-work-in-Java
How does Integer.parseInt() work in Java? - Quora
Answer (1 of 4): Usually this is done like this: * init result with 0 * for each character in string do thisresult = result * 10get the digit from the character ('0' is 48 ASCII (or 0x30), so just subtract that from the character ASCII code to get the digit)add the digit to the result * retur...
๐ŸŒ
Oracle
docs.oracle.com โ€บ javase โ€บ 7 โ€บ docs โ€บ api โ€บ java โ€บ lang โ€บ Integer.html
Integer (Java Platform SE 7 )
+ DecimalNumeral, HexDigits, and ... sign and/or radix specifier ("0x", "0X", "#", or leading zero) is parsed as by the Integer.parseInt method with the indicated radix (10, 16, or 8)....
๐ŸŒ
Java67
java67.com โ€บ 2015 โ€บ 08 โ€บ 2-ways-to-parse-string-to-int-in-java.html
2 ways to parse String to int in Java - Example Tutorial | Java67
Learn Java and Programming through articles, code examples, and tutorials for developers of all levels. ... Java provides Integer.parseInt() method to parse a String to an int value, but that's not the only way to convert a numeric String to int in Java.