Java Convert Hexadecimal String to Long

Tags: Hexadecimal long

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

How to convert Hexadecimal String to Long in Java

In Java, with a given String in hexadecimal format we can use the Long.decode(String nm) static method to convert it to a long value as the following example Java code.

ConvertHexToLongExample.java

public class ConvertHexToLongExample {
    public static void main(String... args) {
        String hexValue1 = "0xf423f"; // String start with 0x
        String hexValue2 = "0Xfffff"; // String start with 0X
        String hexValue3 = "#300"; // String start with #

        long longValue1 = Long.decode(hexValue1);
        long longValue2 = Long.decode(hexValue2);
        long longValue3 = Long.decode(hexValue3);

        System.out.println("hexValue1: " + hexValue1);
        System.out.println("longValue1: " + longValue1);
        System.out.println("\nhexValue2: " + hexValue2);
        System.out.println("longValue2: " + longValue2);
        System.out.println("\nhexValue3: " + hexValue3);
        System.out.println("longValue3: " + longValue3);
    }
}
The output as below.
hexValue1: 0xf423f
longValue1: 999999

hexValue2: 0Xfffff
longValue2: 1048575

hexValue3: #300
longValue3: 768

Happy Coding 😊

Java Convert Long to Hexadecimal String

Java Convert Long to Octal String

Java Convert Octal String to Long

Java Convert long to byte