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.
🌐
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 ·
🌐
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.
🌐
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.
🌐
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.
🌐
Tutorialspoint
tutorialspoint.com › java › lang › integer_parseint.htm
Java - Integer parseInt() method
package com.tutorialspoint; public class IntegerDemo { public static void main(String[] args) { String str = "0x3"; /* returns an Integer object holding the int value represented by string str */ System.out.println("Number = " + Integer.parseInt(str)); } } Let us compile and run the above program, this will produce the following result − · Exception in thread "main" java.lang.NumberFormatException: For input string: "0x3" at java.lang.NumberFormatException.forInputString(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at com.tutorialspoint.IntegerDemo.main(IntegerDemo.java:10)
🌐
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 ...
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.
🌐
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 ...
🌐
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.
🌐
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)....
🌐
Stack Overflow
stackoverflow.com › questions › 59908118 › what-does-integer-parseint-do
java - What does Integer.parseInt do? - Stack Overflow
Integer.parseInt takes a String and converts it to an int. For example: Integer.parseInt("21"); returns 21.
🌐
Coderanch
coderanch.com › t › 614543 › java › Meaning-parseint-method
Meaning and Use of parseint method (Beginning Java forum at Coderanch)
When you use two parameters for the parseInt method, the first takes your string, tries to parse it, and the second is a radix - which is apparently the number of unique digits used to represent a number. So, "444" parsed using 5 unique digits is apparently 124. Unsure of the exact math but ...