0

一个while循环遍历一个字符串,一次接受8个字符的子字符串,获取每个字符的ASCII码并将每个字节转换为其二进制表示,作为8个字符所以(8 * 8)数组,每8个字符,这里是代码:

消息文件包含:1234567800000008

所以 while 循环运行两次,因为有 16 个字符,代码运行正确,直到在最后一个 for 循环的输入块中输入二进制值。当我在数组列表中添加输入块时,当我再次从数组列表中获取()它们时,它们不是相同的值

        while(Message.length() != 0){
            substr=Message.substring(0, 8);
            bytes = substr.getBytes("ASCII");
            int[][] inputblock = new int[bytes.length][bytes.length];

                for(int i=0; i<bytes.length;i++){
                    binString =Integer.toBinaryString(bytes[i]);

                        while (binString.length() < 8) {binString = "0" + binString;}

                      for(int j=0; j<bytes.length;j++){
                           int x=Character.digit(binString.charAt(j), 2);
                                inputblock[i][j] = x;

                            }
                          }     
             input.add(inputblock);  //// This is supposed to add each 64 bit (8*8) 
          Message=Message.replace(substr, "");        //Array to arraylist input. 
    System.out.print(substr + "+");


    }

出于某种原因,当我打印arraylist 的内容时,它打印出不是二进制表示的实际字符.. 我不知道为什么?

输出内容

for (int i =0;i<input.size();i++){
     int[][] tmp = input.get(i);

     for(int j=0;j<input.size();j++){
         System.out.print(tmp[i][j]); }
       System.out.print("\n");
    }

输出:12345678+00000008+00 00

4

2 回答 2

0

You mention "getting elements from an ArrayList" in your question, but I can't see you reading from input anywhere (assuming that's the list in this case).

The value printed out will always be substr followed by a literal + character, as this is the only thing you ask your program to print out (in the final line of the while loop). The value of substr is set at the start of the loop to be the eight actual characters, so this behaviour seems appropriate.

If you want to print out the binary values, you'll need to convert input into a string in whatever format you're after, and pass that to System.out.println().


EDIT with the extra information of how you're displaying the relevant output, there is a bug in your input-printing method.

You're declaring both for loops as:

for(int i=0;i<input.size();i++)

(and the same for j). So both counter variables go from 0 to input.size - which is 2, as there are 2 characters. As a result, you print out the first two bits of the first two bytes - 00 and 00.

What you ought to be doing AIUI is iterating fully over both dimensions of the tmp array:

for(int i = 0; i < input.size(); i++) {
   int[][] tmp = input.get(i);

   for(int j = 0; j < tmp.length; j++) {
      for (int k = 0; k < tmp[j].length; k++) {
         System.out.print(tmp[j][k]);
      }
      System.out.println();
   }
   System.out.println();
}
于 2012-11-20T10:26:41.650 回答
0

一切都很好,除了下面的代码......

for (int i =0;i<input.size();i++){
     int[][] tmp = input.get(i);

     for(int j=0;j<input.size();j++){
         System.out.print(tmp[i][j]); }
       System.out.print("\n");
}

将此代码替换为以下代码...

for (int[][] tmp :input.size();){

    for(int i=0; i< tmp.length; i++)
        for(int j=0;j<tmp.length;j++){
            System.out.print(tmp[i][j]); 
        }
       System.out.print("\n");
    }
}

其他一切都是正确的

于 2012-11-20T11:20:27.137 回答