Core Java

Java Decimal to Hexadecimal

In Java, numerical representation plays a crucial role, and two common ways to represent numbers are decimal and hexadecimal.

Decimal numbers are the standard base-10 numerical system we use every day. They consist of digits 0 to 9 and are used extensively in Java for everyday calculations and storage.

Hexadecimal numbers, on the other hand, are base-16 numbers represented using digits 0-9 and letters A. They are widely employed in Java for various purposes, such as memory addresses, bitwise operations, and encoding binary data. Understanding both decimal and hexadecimal numbering systems is essential for effective programming in Java, as they offer versatility and efficiency for different tasks.

1. Representing Numbers of Different Bases in Java

Java, a versatile programming language, allows you to work with numbers in various bases, such as decimal, hexadecimal, octal, and binary. Understanding how to represent numbers in different bases is essential for tasks ranging from memory management to bitwise operations.

DecimalDefault base-10 representation with digits decimalNumber = 42;
HexadecimalBase-16 with digits 0-9 and A-F. Prefix with “0x” or “0X” in hexadecimalNumber = 0x2A;
OctalBase-8 with digits 0-7. Prefix with a leading octalNumber = 052;
BinaryBase-2 with only digits 0 and 1. Use the “0b” or “0B” prefix (Java 7 and later).int binaryNumber = 0b1010;

2. Understanding Decimal Numbers in Java

Decimal numbers are a fundamental concept in Java programming. They are the numbers we use in our daily lives, represented in the familiar base-10 numbering system. In Java, decimal numbers are represented using the standard base-10 system, which includes digits from 0 to 9. These numbers can be integers, like 1, 42, or 1000, or they can be floating-point numbers, such as 3.14 or 0.001.

2.1 Usage of Decimal Numbers

  • Arithmetic Operations: You can perform addition, subtraction, multiplication, and division with decimal numbers.
  • Storage: Decimal values are stored in variables and data structures for calculations and data manipulation.
  • Input and Output: When interacting with users or external data sources, decimal numbers are commonly used for input and output.

3. Understanding Hexadecimal Numbers in Java

Hexadecimal numbers are a vital part of Java programming, offering a compact and convenient way to represent binary data and memory addresses. In Java, hexadecimal numbers are represented using the base-16 numbering system. They consist of digits from 0 to 9 and letters from A to F. Each hexadecimal digit represents four bits, allowing for a concise representation of binary data.

3.1 Usage of Hexadecimal Numbers

  • Memory Addresses: Memory addresses and pointers are often represented in hexadecimal format for low-level system programming.
  • Bitwise Operations: Hexadecimal numbers are used in bitwise operations, such as setting or clearing specific bits in binary data.
  • Color Codes: In graphical applications, colors are sometimes specified using hexadecimal values for red, green, and blue components.
  • Binary Data: When working with binary data, such as files or network packets, hexadecimal is a convenient format for visualization and manipulation.

4. Decimal vs. Hexadecimal Numbers

Digits0-90-9, A-F
Representation123, 42.750x7B, 0x2A.C
Bit RepresentationBinary digits represent valuesEach hexadecimal digit represents 4 bits
Common UsageEveryday calculationsMemory addresses, binary data
ConversionDecimals to Hex: Use Integer.toHexString()
Hex to Decimals: Use Integer.parseInt(“0x7B”, 16)
Hex to Dec: Use Integer.parseInt(“7B”, 16)
Dec to Hex: Use Integer.toHexString(123)

5. Java Decimal to Hexadecimal

To convert a decimal number to hexadecimal in Java, you can use the Integer.toHexString() method. Here’s how you can do it:

public class DecimalToHexadecimal {
    public static void main(String[] args) {
        // Decimal number to convert
        int decimalNumber = 123;

        // Convert to hexadecimal
        String hexadecimalValue = Integer.toHexString(decimalNumber);

        // Display the result
        System.out.println("Decimal " + decimalNumber + " is equivalent to Hexadecimal: " + hexadecimalValue);

In this example, we first define the decimal number you want to convert (decimalNumber). Then, we use the Integer.toHexString() method to convert it to a hexadecimal string. Finally, we print the result, which will show the decimal number and its equivalent hexadecimal representation. When you run this code, it will convert the decimal number 123 to hexadecimal, and the output will be:

Console Output

Decimal 123 is equivalent to Hexadecimal: 7b

You can replace the decimalNumber variable with any decimal value you want to convert to hexadecimal.

6. Java Hexadecimal to Decimal

To convert a hexadecimal number to a decimal in Java, you can use the Integer.parseInt() method with a radix of 16. Here’s how you can do it:

public class HexadecimalToDecimal {
    public static void main(String[] args) {
        // Hexadecimal number to convert (without "0x" prefix)
        String hexadecimalValue = "7B";

        // Convert to decimal
        int decimalNumber = Integer.parseInt(hexadecimalValue, 16);

        // Display the result
        System.out.println("Hexadecimal " + hexadecimalValue + " is equivalent to Decimal: " + decimalNumber);

In this example, we define the hexadecimal number you want to convert as a string (hexadecimalValue). Then, we use the Integer.parseInt() method with a radix of 16 to convert it to a decimal integer. Finally, we print the result, which will show the hexadecimal number and its equivalent decimal representation. When you run this code with the given hexadecimal value “7B,” the output will be:

Console Output

Hexadecimal 7B is equivalent to Decimal: 123

You can replace the hexadecimalValue variable with any hexadecimal value you want to convert to decimal.

7. Conclusion

In the world of Java programming, having the knowledge and skills to work with numbers in different bases is a valuable asset. Let’s summarize what we’ve learned:

  • Decimal Numbers: These are the default numeric representation in Java, using base-10 and digits from 0 to 9. They are used for most everyday calculations.
  • Hexadecimal Numbers: Hexadecimal numbers use base-16 and include digits 0-9 and letters A-F. They are essential for representing memory addresses, bitwise operations, and binary data.
  • Octal Numbers: Octal numbers use base-8 and include digits 0-7. They are less common but still useful in specific situations.
  • Binary Numbers: Binary numbers use base-2 and include only digits 0 and 1. They are ideal for dealing with low-level binary data and bitwise manipulation.

Furthermore, Java provides convenient methods for converting numbers between these bases, making it relatively straightforward to work with each of them. Whether you are a beginner or an experienced Java developer, having a solid understanding of number bases will enhance your programming capabilities.

As you continue to explore the world of Java development, remember that choosing the right number base for your specific task can significantly improve the efficiency and clarity of your code. So, embrace the versatility of Java and leverage the power of different number bases when the need arises.


An experience full-stack engineer well versed with Core Java, Spring/Springboot, MVC, Security, AOP, Frontend (Angular & React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Back to top button