Java程序示例,展示十六进制的使用方法

2024-03-04 0 2,461

Java程序示例,展示十六进制的使用方法

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.

十六进制数从0到9相当于十进制数从0到9。

此外,A代表10,B代表11,C代表12,D代表13,E代表14,F代表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

  • 将长数字转换为十六进制数字

转化的基础知识

Consider any decimal value and convert it into a hexadecimal number system.

Let us consider the decimal number 775 .

的中文翻译为:

No.

Quotient

余数

Hex Value

十六进制值

775/16

48

7

7

48/16

3

0

0

3/16

0

3

3

因此,相应的十六进制值为307

Let us consider another decimal number 1256.

的中文翻译为:

No.

Quotient

余数

Hex Value

十六进制值

1256/16

78

8

8

78/16

4

14

E

4/16

0

4

4

因此,相应的十六进制值 = 4E8

Example 1

的翻译为:

示例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.

Example 2

的中文翻译为:

示例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

这是另一个演示十六进制数使用方法的程序。在这里,使用 toHexString 方法将十进制数转换为相应的十六进制数。

Example 3

的翻译为:

示例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

这个程序是为了演示十六进制数系统的工作原理而编写的。在这里,一个十六进制数被赋给一个变量,并使用库函数Long.toHexString将其转换为相应的长整数。

Example 5

让我们看一个Java程序,使用库函数 toHexString 将一个长整型数转换为其对应的十六进制数。

// 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

本文讲解了在Java中使用十六进制的用法。我们展示了四种使用十六进制值的方法。我们还看到了五种不同的实现方式,以了解其用法。

资源下载此资源下载价格为1小猪币,终身VIP免费,请先
由于本站资源来源于互联网,以研究交流为目的,所有仅供大家参考、学习,不存在任何商业目的与商业用途,如资源存在BUG以及其他任何问题,请自行解决,本站不提供技术服务! 由于资源为虚拟可复制性,下载后不予退积分和退款,谢谢您的支持!如遇到失效或错误的下载链接请联系客服QQ:442469558

:本文采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可, 转载请附上原文出处链接。
1、本站提供的源码不保证资源的完整性以及安全性,不附带任何技术服务!
2、本站提供的模板、软件工具等其他资源,均不包含技术服务,请大家谅解!
3、本站提供的资源仅供下载者参考学习,请勿用于任何商业用途,请24小时内删除!
4、如需商用,请购买正版,由于未及时购买正版发生的侵权行为,与本站无关。
5、本站部分资源存放于百度网盘或其他网盘中,请提前注册好百度网盘账号,下载安装百度网盘客户端或其他网盘客户端进行下载;
6、本站部分资源文件是经压缩后的,请下载后安装解压软件,推荐使用WinRAR和7-Zip解压软件。
7、如果本站提供的资源侵犯到了您的权益,请邮件联系: 442469558@qq.com 进行处理!

猪小侠源码-最新源码下载平台 Java教程 Java程序示例,展示十六进制的使用方法 http://www.20zxx.cn/808842/xuexijiaocheng/javajc.html

猪小侠源码,优质资源分享网

常见问题
  • 本站所有资源版权均属于原作者所有,均只能用于参考学习,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担
查看详情
  • 最常见的情况是下载不完整: 可对比下载完压缩包的与网盘上的容量,建议提前注册好百度网盘账号,使用百度网盘客户端下载
查看详情

相关文章

官方客服团队

为您解决烦忧 - 24小时在线 专业服务