Here, the usage of Hexadecimal shall be demonstrated through the Java Program.
Let us get acquainted with the term Hexadecimal before seeing a Java program.
The Hexadecimal is a type of number system that has a base value of 16. There are 16 symbols representing hexadecimal numbers. These symbols or values are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, and F. Each digit represents a decimal value.
Hexadecimal numbers from 0 to 9 are equivalent to decimal numbers from 0 to 9.
In addition, A represents 10, B represents 11, C represents 12, D represents 13, E represents 14, and F represents 15.
Demonstrating the usage of the Hexadecimal number system through certain examples:
Converting Decimal numbers to Hexadecimal numbers
Converting Hexadecimal numbers to Decimal numbers
Converting Hexadecimal numbers to long number
Convert long numbers to hexadecimal numbers
Basic knowledge of conversion
Consider any decimal value and convert it into a hexadecimal number system.
Let us consider the decimal number 775 .
No. |
Quotient |
remainder |
The Chinese translation of Hex Value | is: Hex Value |
---|---|---|---|---|
775/16 |
48 |
7 |
7 |
|
48/16 |
3 |
0 |
0 |
|
3/16 |
0 |
3 |
3 |
Therefore, the corresponding hexadecimal value is 307
Let us consider another decimal number 1256.
No. |
Quotient |
remainder |
The Chinese translation of Hex Value | is: Hex Value |
---|---|---|---|---|
1256/16 |
78 |
8 |
8 |
|
78/16 |
4 |
14 |
E |
|
4/16 |
0 |
4 |
4 |
Therefore, the corresponding hexadecimal value = 4E8
Example 1
is translated as:Example 1
Let us see a java program to convert a decimal number into its corresponding Hexadecimal number.
// Java program to convert a decimal // number into a hexadecimal number import java.io.*; public class DectoHexadecimal { // method to convert decimal to hexadecimal number system static void decToHexa(int num) { // char array that stores the hexadecimal number char[] hexaDec = new char[100]; int j = 0; while (num != 0) { // temporary variable to store the remainder int t = 0; // storing the remainder in the temp variable. t = num % 16; // check if temp < 10 if (t < 10) { hexaDec[j] = (char)(t + 48); j++; } else { hexaDec[j] = (char)(t + 55); j++; } num = num / 16; } // hexadecimal number array in reverse order for (int k = j - 1; k >= 0; k--) System.out.print( hexaDec[k]); } public static void main(String[] args){ int num = 4698; System.out.print("Hexadecimal equivalent of " +num+ " is " ); decToHexa(num); } }
Output
Hexadecimal equivalent of 4698 is 125A
This Program is written to demonstrate the working of Hexadecimal number systems. Here, a decimal number is assigned in a variable and the same is converted into a corresponding Hexadecimal number using custom logic.
The Chinese translation ofExample 2
is:Example 2
Let us see a java program to convert a decimal number into its corresponding Hexadecimal number using a predefined function that is a library function toHexString.
// Java Program to demonstrate the Usage of HexaDecimal import java.io.*; public class DecimaltoHex { // Main method public static void main(String[] args){ //Decimal number to be converted int d = 986; // Using the toHexString() method to convert decimal value to its // corresponding hexadecimal value String hexd = Integer.toHexString(d); // Displaying hexaDec value System.out.println("Hexadecimal value of " +d+ " is " + hexd); } }
Output
Hexadecimal value of 986 is 3da
This is another program that demonstrates the use of hexadecimal numbers. Here, the decimal number is converted to the corresponding hexadecimal number using the toHexString method.
Example 3
is translated as:Example 3
Let us see a java program to convert a Hexadecimal number into its corresponding decimal number using a library function parseInt.
// Java program to convert Hexadecimal numbers to corresponding Decimal number import java.io.*; public class HexToDecimal { // Main method public static void main(String[] args) { // Random Hexadecimal number stored in a string String hexd = "4B6A"; // Passing hexd and base 16 as parameters // to parseInt method int dec = Integer.parseInt(hexd, 16); // Displaying the corresponding // decimal value of a hexadecimal number System.out.println("Corresponding Decimal value of" + hexd + " is " + dec); } }
Output
Corresponding Decimal value of4B6A is 19306
This Program is written to demonstrate the working of Hexadecimal number systems. Here, a Hexadecimal number is assigned to a variable and is converted into its corresponding decimal number using a library function integer.parseInt .
Example 4
Let us see a java program to convert a Hexadecimal number into its corresponding long number using a library function Long.toHexString.
// Java Program to demonstrate the Usage of HexaDecimal import java.io.*; public class LongToHex { // Main method public static void main(String[] args) { // Long number to be converted long l = 2028; // Storing the result in a string hexd String hexd = Long.toHexString(l); // Displaying the corresponding hexadecimal value System.out.println("Corresponding Hex value of long number " +l+ " is " + hexd); } }
Output
Corresponding Hex value of long number 2028 is 7ec
This program was written to demonstrate how the hexadecimal number system works. Here, a hexadecimal number is assigned to a variable and converted to the corresponding long integer using the library function Long.toHexString.
Example 5
Let us look at a Java program that uses the library function toHexString to convert a long integer to its corresponding hexadecimal number.
// Java Program to Illustrate the Usage of HexaDecimal by converting a hexadecimal value into long value import java.io.*; public class HexToLong { // Main method public static void main(String[] args) { // Hexadecimal number to be converted String hs = "7850"; // passing hs and base 16 as parameters // to parseLong function long l = Long.parseLong(hs, 16); // Displaying the corresponding hexadecimal value System.out.println("Corresponding Long value of hexadecimal no. " +hs+ " is " + l); } }
Output
Corresponding Long value of hexadecimal no. 7850 is 30800
This Program is written to demonstrate the working of Hexadecimal number systems. Here, a hexadecimal number is assigned to a variable and is converted into its corresponding long number using a library function Long.parseLong
This article explains the use of hexadecimal in Java. We show four ways to use hexadecimal values. We also saw five different implementations to understand its usage.
The above is the detailed content of Java program example showing how to use hexadecimal. For more information, please follow other related articles on the PHP Chinese website!

Start Spring using IntelliJIDEAUltimate version...

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

Java...

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to set the SpringBoot project default run configuration list in Idea using IntelliJ...


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

WebStorm Mac version
Useful JavaScript development tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 English version
Recommended: Win version, supports code prompts!