简体   繁体   English

为什么我的Bit String to Hex String代码无法打印结果?

[英]Why isn't my Bit String to Hex String code printing the result?

I am trying to print the result of a bit string to hex string conversion but it prints nothing. 我正在尝试将位字符串的结果打印为十六进制字符串转换,但不打印任何内容。 In other words 000110011101 should print "19d" are my temp variables the issue? 换句话说,000110011101应该打印“ 19d”是我的临时变量吗?

Thanks in advance for any assistance, this is my code: 在此先感谢您的协助,这是我的代码:

public static void BinaryToHex() {
        Scanner scanner = new Scanner(System.in);
        String bitString = "";
        String hexString = "";

    String[] hex = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C",
            "D", "E", "F" };
    String[] binary = { "0000", "0001", "0010", "0011", "0100", "0101", "0110",
            "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111" };

    System.out.println("Enter a bit string: ");
    bitString = scanner.next();

    for (int i = 0; i < bitString.length(); i++) {
        char temp = bitString.charAt(i);
        String temp2 = "" + temp + "";
        for (int j = 0; j < binary.length; j++) {
            if (temp2.equalsIgnoreCase(binary[j])) {
                hexString = hexString + hex[j];
            }
        }
    }

    System.out.print("The equivalent hex string is ");
    System.out.println(hexString);
}
for (int i = 0; i < bitString.length(); i++) {
        char temp = bitString.charAt(i);
        String temp2 = "" + temp + "";
        for (int j = 0; j < binary.length; j++) {
            if (temp2.equalsIgnoreCase(binary[j])) {
                hexString = hexString + hex[j];
            }
        }
    }

In this loop you are trying to take one character of a bitString at a time and compare it with the input of binary[] array which has all input of length 4 . 在此循环中,您尝试一次获取一个bitString的一个字符,并将其与所有长度为4的binary []数组的输入进行比较。 So basically you are trying to comparing 1 length element with 4 length element which is never going to be true. 因此,基本上,您正在尝试将1长度元素与4长度元素进行比较,这永远都是不正确的。

Therefore HexString will never get changed and it will print same as you have initialized it.So you are getting nothing while executing the code. 因此HexString将永远不会被更改,它的输出将与初始化时相同,因此在执行代码时一无所获。

You can have a substring of bitString having 4 lengths and store it in temp2 then this code will work and for every 4 bits there is a HexCharacter so you can't make 1 bit number into binary number. 您可以有一个bitString的子字符串,长度为4 ,并将其存储在temp2中,然后此代码将起作用,并且每4位有一个HexCharacter,因此您不能将1位数字转换为二进制数字。

So as per your logic , bitString length should be of multiples of 4 otherwise it won't give corresponding hex character from hex array. 因此,按照您的逻辑, bitString length应为4的倍数,否则它将不会从hex数组中给出相应的十六进制字符。

Replace the for loop in your code with this. 以此替换代码中的for循环。

if(bitString.length()%4!=0)
    System.out.println("Please Enter Valid Input.");
else
{
    for (int i = 0; i < bitString.length()/4; i++) 
    {
        String temp2 = bitString.substring(4*i,4*(i+1));
        for (int j = 0; j < binary.length; j++)
            {
            if (temp2.equalsIgnoreCase(binary[j]))
                {
                hexString = hexString + hex[j];
            }
        }
    }
    System.out.print("The equivalent hex string is ");
    System.out.println(hexString);
}

Your temp2 String is re-defined every time inside the outer for-loop, which loops through the char s of the input String. 您的temp2字符串被重新限定的外for循环,它通过环内的每个时间char的输入字符串的第 This means that temp2 never actually contains more than a single symbol (because it gets reset every iteration of the loop), and therefore can never equal any of the Strings in the binary array. 这意味着temp2实际上不会包含多个符号(因为它在循环的每次迭代中都会重置),因此永远不能等于binary数组中的任何字符串。

You'll want to define the temp2 String once first as String temp2 = "" outside the loop, and then inside the loop append temp to it. 您将需要先在循环外部将temp2字符串定义为String temp2 = "" ,然后在循环内部将temp附加到它。

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

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