📌  相关文章
📜  将二进制转换为十六进制的Java程序

📅  最后修改于: 2022-05-13 01:55:41.128000             🧑  作者: Mango

将二进制转换为十六进制的Java程序

顾名思义,十六进制数字系统包括 16 个实体。这 16 个实体由10 个数字组成,0-9也代表十六进制系统的前 10 个数字。剩下的6个数字,我们用A到F的英文字母来表示数字10到15。需要注意的是,十六进制中最小的数字是0,最大的数字是15,用F表示。一个十六进制的数字可以通过将 4 位数字摁在一起构成十六进制数的单个字符,从二进制数推导出来。

例子:

Input: 11011111
Output: DF
 
Input: 10001101
Output: 8D
  • 在上面的示例中,二进制数 10001101 可以分解为 4 位的块,例如 1000 和 1101,它们充当相应十六进制数的 2 个字符。
  • 结果的十六进制数将是 8D,其中每个字符是通过计算其在十进制系统中的相应值并用字母替换它来确定的,如果它是在这种情况下 D 表示 13 的两位数,则十六进制系统也被称为作为基数 16。

对于二进制到十六进制的转换,我们将使用以下两种方法:

  1. 使用toHexString()内置Java方法
  2. 反复求余数并将转换后的十进制数除以16

方法一:

使用这种方法,我们首先将二进制数转换为以整数形式存储的十进制数。然后,我们只需使用Java的toHexString()方法来生成所需的输出字符串。

句法 :

public static String toHexString(int num)

范围:

  • num - 此参数指定要转换的数字
    到十六进制字符串。数据类型是int。

返回值:该函数将 int 参数的字符串表示形式作为基数为 16 的无符号整数返回。

算法 :

  1. 将二进制数转换为十进制数。
  2. 要将二进制数转换为十进制数,首先,通过除以 10 得到余数来提取每个数字。
  3. 接下来,将此数字与 2 的递增幂相乘。
  4. 继续将原始二进制数除以 10,以消除每次迭代中的最后一位。
  5. 获得十进制数后,只需使用 toHexString() 方法即可获得所需的输出。

例子:

Java
// Java program to convert binary to hexadecimal
 
class GFG {
 
    // method to convert binary to decimal
    int binaryToDecimal(long binary)
    {
 
        // variable to store the converted
        // binary number
        int decimalNumber = 0, i = 0;
 
        // loop to extract the digits of the binary
        while (binary > 0) {
 
            // extracting the digits by getting
            // remainder on dividing by 10 and
            // multiplying by increasing integral
            // powers of 2
            decimalNumber
                += Math.pow(2, i++) * (binary % 10);
 
            // updating the binary by eliminating
            // the last digit on division by 10
            binary /= 10;
        }
 
        // returning the decimal number
        return decimalNumber;
    }
 
    // method to convert decimal to hexadecimal
    String decimalToHex(long binary)
    {
        // variable to store the output of the
        // binaryToDecimal() method
        int decimalNumber = binaryToDecimal(binary);
 
        // converting the integer to the desired
        // hex string using toHexString() method
        String hexNumber
            = Integer.toHexString(decimalNumber);
 
        // converting the string to uppercase
        // for uniformity
        hexNumber = hexNumber.toUpperCase();
 
        // returning the final hex string
        return hexNumber;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
 
        // instantiating the class
        GFG ob = new GFG();
 
        long num = 10011110;
       
        // calling and printing the output
        // of decimalToHex() method
        System.out.println("Inputted number : " +num);
        System.out.println(ob.decimalToHex(10011110));
    }
}


Java
// Java program to convert binary to hexadecimal
 
class GFG {
 
    // method to convert binary to decimal
    int binaryToDecimal(long binary)
    {
 
        // variable to store the converted binary
        int decimalNumber = 0, i = 0;
 
        // loop to extract digits of the binary
        while (binary > 0) {
 
            // extracting each digit of the binary
            // by getting the remainder of division
            // by 10 and multiplying it by
            // increasing integral powers of 2
            decimalNumber
                += Math.pow(2, i++) * (binary % 10);
 
            // update condition of dividing the
            // binary by 10
            binary /= 10;
        }
 
        // returning the decimal
        return decimalNumber;
    }
 
    // method to convert decimal to hex
    String decimalToHex(long binary)
    {
 
        // variable to store the output of
        // binaryToDecimal() method
        int decimalNumber = binaryToDecimal(binary);
 
        // character array to represent double
        // digit remainders
        char arr[] = { 'A', 'B', 'C', 'D', 'E', 'F' };
 
        // variable to store the remainder on
        // division by 16
        int remainder, i = 0;
 
        // declaring the string that stores the
        // final hex string
        String hexNumber = "";
 
        // loop to convert decimal to hex
        while (decimalNumber != 0) {
 
            // calculating the remainder of decimal
            // by dividing by 16
            remainder = decimalNumber % 16;
 
            // checking if the remainder is >= 10
            if (remainder >= 10)
               
                // replacing with the corresponding
                // alphabet from the array
                hexNumber = arr[remainder - 10] + hexNumber;
           
            else
               
                hexNumber = remainder + hexNumber;
 
            // update condition of dividing the
            // number by 16
            decimalNumber /= 16;
        }
 
        // returning the hex string
        return hexNumber;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
 
        // instantiating the class
        GFG ob = new GFG();
 
        long num =11000011;
       
        // printing and calling the
        // decimalToHex() method
        System.out.println("Input : "+num);
        System.out.println("Output : " +ob.decimalToHex(num));
    }
}


输出
Inputted number : 10011110
9E

方法二:

  • 在第二种方法下,我们再次将二进制数转换为十进制数。
  • 然后我们不断地除法并得到这个十进制数的余数,以获得我们在原始二进制数中可以找到的每组 4 位的单个字符。

算法:

  1. 使用上述算法中的步骤 2-4 将二进制转换为十进制。
  2. 接下来,以十进制数变为0的终止条件和每次迭代中十进制数除以16的更新条件运行一个while循环。
  3. 在每次迭代中,将数字除以 16 得到余数。
  4. 组成一个新的 String 并继续添加除以 16 后剩余的字符。
  5. 如果余数大于或等于 10,则根据余数将其替换为字母 AF。

例子:

Java

// Java program to convert binary to hexadecimal
 
class GFG {
 
    // method to convert binary to decimal
    int binaryToDecimal(long binary)
    {
 
        // variable to store the converted binary
        int decimalNumber = 0, i = 0;
 
        // loop to extract digits of the binary
        while (binary > 0) {
 
            // extracting each digit of the binary
            // by getting the remainder of division
            // by 10 and multiplying it by
            // increasing integral powers of 2
            decimalNumber
                += Math.pow(2, i++) * (binary % 10);
 
            // update condition of dividing the
            // binary by 10
            binary /= 10;
        }
 
        // returning the decimal
        return decimalNumber;
    }
 
    // method to convert decimal to hex
    String decimalToHex(long binary)
    {
 
        // variable to store the output of
        // binaryToDecimal() method
        int decimalNumber = binaryToDecimal(binary);
 
        // character array to represent double
        // digit remainders
        char arr[] = { 'A', 'B', 'C', 'D', 'E', 'F' };
 
        // variable to store the remainder on
        // division by 16
        int remainder, i = 0;
 
        // declaring the string that stores the
        // final hex string
        String hexNumber = "";
 
        // loop to convert decimal to hex
        while (decimalNumber != 0) {
 
            // calculating the remainder of decimal
            // by dividing by 16
            remainder = decimalNumber % 16;
 
            // checking if the remainder is >= 10
            if (remainder >= 10)
               
                // replacing with the corresponding
                // alphabet from the array
                hexNumber = arr[remainder - 10] + hexNumber;
           
            else
               
                hexNumber = remainder + hexNumber;
 
            // update condition of dividing the
            // number by 16
            decimalNumber /= 16;
        }
 
        // returning the hex string
        return hexNumber;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
 
        // instantiating the class
        GFG ob = new GFG();
 
        long num =11000011;
       
        // printing and calling the
        // decimalToHex() method
        System.out.println("Input : "+num);
        System.out.println("Output : " +ob.decimalToHex(num));
    }
}
输出
Input : 11000011
Output : C3