将二进制字符串转换为十六进制字符串 JAVA [英] Converting binary string to a hexadecimal string JAVA

查看:49
本文介绍了将二进制字符串转换为十六进制字符串 JAVA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我也想将我的二进制(在字符串中)转换为十六进制字符串,这只是一个程序片段,因为这个程序只是另一个更大程序的一部分:

I want to convert my binary(which is in string) to hexadecimal string also, this is just a program fragment since this program is just a part of another bigger program:

//the variable name of the binary string is: "binary"
int digitNumber = 1;
    int sum = 0;
    int test = binary.length()%4;
    if(test!=0) {
        binary = padLeft(binary, test);
    }
    for(int i = 0; i < binary.length(); i++){
        if(digitNumber == 1)
            sum+=Integer.parseInt(binary.charAt(i) + "")*8;
        else if(digitNumber == 2)
            sum+=Integer.parseInt(binary.charAt(i) + "")*4;
        else if(digitNumber == 3)
            sum+=Integer.parseInt(binary.charAt(i) + "")*2;
        else if(digitNumber == 4 || i < binary.length()+1){
            sum+=Integer.parseInt(binary.charAt(i) + "")*1;
            digitNumber = 0;
            if(sum < 10)
                System.out.print(sum);
            else if(sum == 10)
                System.out.print("A");
            else if(sum == 11)
                System.out.print("B");
            else if(sum == 12)
                System.out.print("C");
            else if(sum == 13)
                System.out.print("D");
            else if(sum == 14)
                System.out.print("E");
            else if(sum == 15)
                System.out.print("F");
            sum=0;
        }
        digitNumber++;  
    }
    public static String padLeft(String s, int n) {
        return String.format("%0$"+n+"s", s);
    }//i added this for padding

问题是我不知道填充是否有效,但我确信这个程序返回错误的二进制字符串的十六进制转换我正在尝试这样做:

the problem is that i dont know if the padding works but i am sure that this program return a wrong hexadecimal conversion of the binary string I am trying to do this:

http://www.wikihow.com/Convert-Binary-to-Hexadecimal

PS:我需要实现它(不使用任何内置函数)

推荐答案

如果您不必自己实现该转换,则可以使用现有代码:

If you don't have to implement that conversion yourself, you can use existing code :

int decimal = Integer.parseInt(binaryStr,2);
String hexStr = Integer.toString(decimal,16);

如果你必须自己实现,你的代码有几个问题:

If you must implement it yourself, there are several problems in your code :

  1. 循环应该从 0 迭代到 binary.length()-1(假设字符串的第一个字符代表最高有效位).
  2. 您隐含地假设您的二进制字符串对于某个整数 x 有 4*x 个字符.如果这不是真的,那么您的算法就会中断.你应该用零填充你的字符串以获得这样长度的字符串.
  3. sum 必须在您输出每个十六进制数字后重置为 0.
  4. System.out.print(digitNumber); - 这里应该打印 sum,而不是 digitNumber.
  1. The loop should iterate from 0 to binary.length()-1 (assuming the first character of the String represents the most significant bit).
  2. You implicitly assume that your binary String has 4*x charcters for some integer x. If that's not true, your algorithm breaks. You should left pad your String with zeroes to get a String of such length.
  3. sum must be reset to 0 after each hex digit you output.
  4. System.out.print(digitNumber); - here you should print sum, not digitNumber.

这是大部分固定代码的外观:

Here's how the mostly fixed code looks :

    int digitNumber = 1;
    int sum = 0;
    String binary = "011110101010";
    for(int i = 0; i < binary.length(); i++){
        if(digitNumber == 1)
            sum+=Integer.parseInt(binary.charAt(i) + "")*8;
        else if(digitNumber == 2)
            sum+=Integer.parseInt(binary.charAt(i) + "")*4;
        else if(digitNumber == 3)
            sum+=Integer.parseInt(binary.charAt(i) + "")*2;
        else if(digitNumber == 4 || i < binary.length()+1){
            sum+=Integer.parseInt(binary.charAt(i) + "")*1;
            digitNumber = 0;
            if(sum < 10)
                System.out.print(sum);
            else if(sum == 10)
                System.out.print("A");
            else if(sum == 11)
                System.out.print("B");
            else if(sum == 12)
                System.out.print("C");
            else if(sum == 13)
                System.out.print("D");
            else if(sum == 14)
                System.out.print("E");
            else if(sum == 15)
                System.out.print("F");
            sum=0;
        }
        digitNumber++;  
    }

输出:

7AA

这仅在二进制位数可被 4 整除时才有效,因此您必须添加左侧 0 填充作为 preliminray 步骤.

This will work only if the number of binary digits is divisable by 4, so you must add left 0 padding as a preliminray step.

这篇关于将二进制字符串转换为十六进制字符串 JAVA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆