Java Convert Hexadecimal String to short

Tags: Hexadecimal short

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

How to convert hexadecimal String to short in Java

In Java, with a given String in hexadecimal format we can convert it to a short value using the Short.decode(String nm) static method as the example Java code below.

ConvertHexToShortExample1.java

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

        // Convert hexadecimal String to short
        short shortValue1 = Short.decode(hexValue1);
        short shortValue2 = Short.decode(hexValue2);
        short shortValue3 = Short.decode(hexValue3);

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

hexValue2: 0X200
shortValue2: 512

hexValue3: #300
shortValue3: 768

Happy Coding 😊

Java Convert short to Hexadecimal String

Java Convert Hexadecimal String to Integer

Java Convert Integer to Hexadecimal String