Maison > Java > tutoriel Java > le corps du texte

Java程序:十六进制转十进制转换

WBOY
Libérer: 2023-08-26 13:01:05
avant
3696 人浏览过

Java程序:十六进制转十进制转换

数字系统有四种类型:二进制、八进制、十进制和十六进制,基值分别为 2、8、10 和 16。基值取决于数字系统包含的位数。例如,二进制数系统仅包含 0 和 1 两个数字,因此其基数为 2。

在本文中,我们将讨论十六进制和十进制数字系统。另外,我们将编写java程序将十六进制数转换为十进制数。

十六进制和十进制数字系统

十六进制数字系统

它代表0到9、A到F的数字。总共有16个数字,它的基值也是16。单个数字的权重是16的幂,每个数字都是16倍比上一张重。 12A16、34B16、45C16 是十六进制的几个示例。在计算机中,颜色代码通常以十六进制形式编写。

假设我们必须存储一个很大的十进制值,如果我们将它存储在二进制计数系统中,那么二进制字符串可能会变得很长。在这种情况下,我们可以使用十六进制数字系统,可以将 4 位二进制存储为 1 位。它缩短了位的长度。

十进制数字系统

它是最常用的数字系统。它有从 0 到 9 的 10 位数字。因此,它的基值为 10。如果没有提到数字的基值,则认为该数字是 10。单个数字的权重为 10 的幂,每个数字都是 10 倍比上一篇更有分量。例如1010、43110、98010等

下表表示所有十六进制数字的二进制和十进制值 -

二进制

十进制

十六进制

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

十六进制到十进制转换

让我们了解如何将十六进制转换为十进制。

示例 1

将十六进制 (54A)16 转换为十进制 -

我们可以通过将每个数字乘以 16n-1 将其转换为十进制,其中 n 是位数。

(54A)16 = 5 * 163-1 + 4 * 162-1 + A * 161-1

= 5 * 162 + 4 * 161 + 10 * 160 【A = 10 的十进制看表】

= 5 * 256 + 64 + 10 [160 等于 1]

= 1280 + 74

= 1354

现在,我们将看到一个java程序,我们将在其中应用上述逻辑将十六进制转换为十进制。

方法一:使用parseInt()方法

它是“Integer”类的静态方法,根据指定的基数返回一个十进制值。它在“java.lang”包中可用。

语法

Integer.parseInt("String", base); 
Copier après la connexion

参数

  • String - 要转换的值

  • Base - 给定值根据给定基数进行转换

示例

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);
   }
}
Copier après la connexion

输出

Decimal value of given Hexadecimal: 1354
Decimal value of given Hexadecimal: 1052
Copier après la connexion
Copier après la connexion

方法二:使用用户自定义方法

在这种方法中,我们将创建一个用户定义的方法 cnvrt() 以及参数“hexNum”。我们将声明并初始化“hexStr”,它将以字符串的形式存储所有十六进制数字。然后,我们将运行一个 for 循环,直到参数“hexNum”的长度。在该循环中,我们将从“hexStr”中获取字符及其索引,然后应用转换逻辑。

在主方法中,我们将使用不同的参数调用方法“cnvrt()”。

示例

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");  
   }
}
Copier après la connexion

输出

Decimal value of given Hexadecimal: 1354
Decimal value of given Hexadecimal: 1052
Copier après la connexion
Copier après la connexion

结论

在本文中,我们了解了数字系统的类型。这些数字系统是任何数学运算的基础。另外,还讨论了两种制作java程序将十六进制数转换为十进制数的方法。

以上是Java程序:十六进制转十进制转换的详细内容。更多信息请关注PHP中文网其他相关文章!

Étiquettes associées:
source:tutorialspoint.com
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!