简体   繁体   English

字符串十六进制二进制(java)

[英]String Hexa-Binary (java)

    public class Try{
        public static void main(String args[]){
            String hex="11000010111100001001111010111000";
             String HexaBin="";
                for (int i = 0; i < hex.length(); i+=4) {
                    String str = hex.substring(i, i+4);
                    if (str=="0000")
                        HexaBin+= "0";
                    else if (str=="0001")
                        HexaBin+="1";
                    else if(str=="0010")
                        HexaBin+="2";
                    else if(str=="0011")
                        HexaBin+="3";
                    else if(str=="0100")
                        HexaBin+="4";
                    else if(str=="0101")
                        HexaBin+="5";
                    else if(str=="0110")
                        HexaBin+="6";
                    else if(str=="0111")
                        HexaBin+="7";
                    else if(str=="1000")
                        HexaBin+="8";
                    else if(str=="1001")
                        HexaBin+="9";
                    else if(str=="1010")
                        HexaBin+="A";
                    else if(str=="0000")
                        HexaBin+="B";
                    else if(str=="1100")
                        HexaBin+="C";
                    else if(str=="1101")
                        HexaBin+="D";
                    else if(str=="1110")
                        HexaBin+="E";
                    else if(str=="1111")
                        HexaBin+="F";

                }
                   System.out.println(HexaBin);
            }
        }

I have a string containing binary numbers. 我有一个包含二进制数字的字符串。 I need to convert it to Hexadecimal without using any built in function. 我需要不使用任何内置函数将其转换为十六进制。 I tried these codes but it wont run. 我尝试了这些代码,但无法运行。 I don't know why. 我不知道为什么

When I run it, it gets terminated. 当我运行它时,它将终止。 what's the problem with these codes. 这些代码有什么问题。

use String.equals() instead of == 使用String.equals()代替==

if (str.equals("0000"))
  HexaBin+= "0";
else if (str.equals("0001"))
  HexaBin+="1";
...

Another point : it will not make you program fail but it is a bad practice to concatene Strings like this : 另一点:不会使您的程序失败,但是将这样的字符串串联起来是一种不好的做法:

 HexaBin+="5";

You should rather declare your HexaBin as a StringBuilder and call StringBuilder.append(): 您应该将HexaBin声明为StringBuilder并调用StringBuilder.append():

//btw, variable names should begin with a lower case character.
StringBuilder hexaBin = new StringBuilder();
...
hexaBin.append("5");

Replace all == with the equals method in your if statements. if语句中将所有==替换为equals方法。

The == operator checks whether the references to the objects are equal or not while the equals method checks the actual contents. ==运算符检查对象的引用是否相等,而equals方法检查实际内容。

Here is a different approach which may be interesting for you own use, but no professor will believe you wrote ;) 这是一种不同的方法,可能对您自己有用,但是没有教授会相信您写的;)

String hex = "11000010111100001001111010111000";
// to long
long val = 0;
for (char ch : hex.toCharArray())
    val = val * 2 + ch - '0';
String hexidecimal = "0123456789ABCDEF";
for (int i = (hex.length() - 1) / 4 * 4; i >= 0; i -= 4)
    System.out.print(hexidecimal.charAt((int) ((val >>> i) & 0xF)));
System.out.println();

prints 版画

62F09EB8

String is object in java so use equals() Instead of == 字符串是Java中的对象,因此请使用equals() 代替 ==

so Change to 因此更改为

if (str.equals("0000")) // for all cases

使用equals()比较两个String是否equality因为Stringreference类型。

As the other posters alredy said, compare Strings with equals() , not with == . 正如其他张贴者所言,将Strings与equals()而不是==

Furthermore, you have a copy-paste-error in your code, "B" will never printed as you compare it with 0000 instead of 1011 . 此外,您的代码中存在复制粘贴错误 ,当您将其与0000而不是1011进行比较时,将永远不会打印“ B”。

If you want to convert numbers between different radices, it's easier to use the functions on the number classes . 如果要在不同的半径之间转换数字,则使用数字类上的函数会更容易。

String hex = "11000010111100001001111010111000";

long num = Long.parseLong(hex, 2);
String hexaBin = Long.toString(num, 16).toUpperCase();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM