Java program: hexadecimal to decimal conversion
There are four types of number systems: binary, octal, decimal, and hexadecimal, with base values 2, 8, 10, and 16 respectively. The base value depends on the number of digits the number system contains. For example, the binary number system contains only two digits, 0 and 1, so its base is 2.
In this article, we will discuss hexadecimal and decimal number systems. Also, we will write java program to convert hexadecimal number to decimal number.
Hexadecimal and Decimal Numbering Systems
Hexadecimal number system
It represents the numbers from 0 to 9, A to F. There are 16 numbers in total, and its base value is also 16. The weight of individual numbers is a power of 16, so each number is 16 times heavier than the previous one. 12A16, 34B16, 45C16 are a few examples of hexadecimal. In computers, color codes are usually written in hexadecimal form.
Suppose we have to store a large decimal value, if we store it in binary counting system, then the binary string may become very long. In this case we can use the hexadecimal number system which can store 4 binary bits as 1 bit. It shortens the bit length.
Decimal number system
It is the most commonly used number system. It has 10 digits from 0 to 9. Therefore, its base value is 10. If a number's base value is not mentioned, the number is assumed to be 10. Individual numbers are weighted as powers of 10, so each number is 10 times more weighty than the last. For example, 1010, 43110, 98010, etc.
The following table represents the binary and decimal values of all hexadecimal numbers -
binary |
decimal |
hexadecimal |
---|---|---|
0001 |
1 |
1 |
0010 |
2 |
2 |
0011 |
3 |
3 |
0100 |
4 |
4 |
0101 |
5 |
5 |
0110 |
6 |
6 |
0111 |
7 |
7 |
1000 |
8 |
8 |
1001 |
9 |
9 |
1010 |
10 |
A |
1011 |
11 |
B |
1100 |
12 |
C |
1101 |
13 |
D |
1110 |
14 |
E |
1111 |
15 |
F |
Hex to Decimal Conversion
Let us understand how to convert hexadecimal to decimal.
Example 1
Convert hexadecimal (54A)16 to decimal -
We can convert each number to decimal by multiplying it by 16n-1, where n is the number of digits.
(54A)16 = 5 * 163-1 4 * 162-1 A * 161-1
= 5 * 162 4 * 161 10 * 160 [A = 10 decimal table]
= 5 * 256 64 10 [160 equals 1]
= 1280 74
= 1354
Now, we will see a java program where we will apply the above logic to convert hexadecimal to decimal.
Method 1: Use parseInt() method
It is a static method of the "Integer" class that returns a decimal value based on the specified base. It is available in the "java.lang" package.
grammar
Integer.parseInt("String", base);
parameter
String - the value to convert
Base - The given value is converted according to the given base
Example
public class Conversion { public static void main(String args[]) { // Converting and storing hexadecimal value to dec1 and dec2 with base 16 int dec1 = Integer.parseInt("54A", 16); int dec2 = Integer.parseInt("41C", 16); System.out.println("Decimal value of given Hexadecimal: " + dec1); System.out.println("Decimal value of given Hexadecimal: " + dec2); } }
Output
Decimal value of given Hexadecimal: 1354 Decimal value of given Hexadecimal: 1052
Method 2: Use user-defined method
In this method, we will create a user-defined method cnvrt() with the parameter "hexNum". We will declare and initialize "hexStr" which will store all hexadecimal digits in the form of a string. We will then run a for loop until the length of the parameter "hexNum". In this loop, we will get the character and its index from "hexStr" and then apply the conversion logic.
In the main method, we will call the method "cnvrt()" with different parameters.
Example
public class Conversion { public static void cnvrt(String hexNum) { // storing all the hexadecimal digits to this string String hexStr = "0123456789ABCDEF"; // converting given argument to uppercase hexNum = hexNum.toUpperCase(); int dec = 0; for (int i = 0; i < hexNum.length(); i++) { char ch = hexNum.charAt(i); // fetching characters sequentially int index = hexStr.indexOf(ch); // fetching index of characters dec = 16 * dec + index; // applying the logic of conversion } System.out.println("Decimal value of given Hexadecimal: " + dec); } public static void main(String args[]) { // calling the function with arguments cnvrt("54A"); cnvrt("41C"); } }
Output
Decimal value of given Hexadecimal: 1354 Decimal value of given Hexadecimal: 1052
in conclusion
In this article, we learned about the types of number systems. These number systems are the basis of any mathematical operation. In addition, two methods of making java programs to convert hexadecimal numbers to decimal numbers are also discussed.
The above is the detailed content of Java program: hexadecimal to decimal conversion. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.

Java is a popular programming language that can be learned by both beginners and experienced developers. This tutorial starts with basic concepts and progresses through advanced topics. After installing the Java Development Kit, you can practice programming by creating a simple "Hello, World!" program. After you understand the code, use the command prompt to compile and run the program, and "Hello, World!" will be output on the console. Learning Java starts your programming journey, and as your mastery deepens, you can create more complex applications.
