To convert decimal number (base 10) to hexadecimal number (base 16) we can use the Integer.toHexString()
method. This method takes an integer as parameter and return a string that represent the number in hexadecimal.
To convert back the number from hexadecimal to decimal number we can use the Integer.parseInt()
method. This method takes two argument, the number to be converted, which is the string that represent a hexadecimal number. The second argument is the radix, we pass 16
which tell the method if the string is a hexadecimal number.
package org.kodejava.lang;
public class ToHexadecimalExample {
public static void main(String[] args) {
// Converting a decimal value to its hexadecimal representation
// can be done using Integer.toHexString() method.
System.out.println(Integer.toHexString(1976));
// On the other hand to convert hexadecimal string to decimal
// we can use Integer.parseInt(string, radix) method,
// where radix for hexadecimal is 16.
System.out.println(Integer.parseInt("7b8", 16));
}
}
Latest posts by Wayan (see all)
- How do I build simple search page using ZK and Spring Boot? - March 8, 2023
- How do I calculate days between two dates excluding weekends and holidays? - January 12, 2023
- How do I discover the quarter of a given date? - January 11, 2023