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

查看:254
本文介绍了二进制字符串转换为十六进制字符串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:我需要实现它(不使用任何内置的功能)

推荐答案

如果你没有自己实施转换,可以利用现有的code:

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);

如果你必须自己实现它,还有你的code以下几个问题:

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


  1. 循环应从0迭代至binary.length() - 1(假设字符串重新presents最显著位的第一个字符)

  2. 您隐含假设你的二进制字符串有一些整数x 4 * X charcters。如果这不是真的,你的算法休息。你应该留下垫零您的字符串得到这样长度的字符串。

  3. 必须每个十六进制数字输出你以后重置为0。

  4. System.out.print(digitNumber); - 在这里你应该打印,而不是 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.

这里的大多是固定的code的外观:

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 divisable,所以你必须添加离开 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天全站免登陆