Java Convert Hexadecimal String to Integer

Tags: Hexadecimal Integer

In this Java core tutorial we learn how to convert a hexadecimal String into an Integer value in Java programming language.

How to convert hex String to Integer in Java

In Java, with a given hexadecimal String we can use the Integer.decode(String nm) static method to convert it to an Integer value as the following Java program.

ConvertHexToIntegerExample.java

public class ConvertHexToIntegerExample {
    public static void main(String... args) {
        String hexValue1 = "0x100";
        String hexValue2 = "0X200";
        String hexValue3 = "#300";

        // Convert Hexadecimal String to Integer
        int intValue1 = Integer.decode(hexValue1);
        int intValue2 = Integer.decode(hexValue2);
        int intValue3 = Integer.decode(hexValue3);

        System.out.println("hexValue1: " + hexValue1);
        System.out.println("intValue1: " + intValue1);
        System.out.println("\nhexValue2: " + hexValue2);
        System.out.println("intValue2: " + intValue2);
        System.out.println("\nhexValue3: " + hexValue3);
        System.out.println("intValue3: " + intValue3);
    }
}
The output as below.
hexValue1: 0x100
intValue1: 256

hexValue2: 0X200
intValue2: 512

hexValue3: #300
intValue3: 768

Happy Coding 😊

Java Convert Hexadecimal String to short

Java Convert short to Hexadecimal String

Java Convert Integer to Hexadecimal String