繁体   English   中英

如何将(00s ... 11s)数组(每个字符的二进制表示)转换为char字符串

[英]How to convert an array of (00s…11s), the binary representation of each character, to a string of char

如何将每个字符的二进制表示形式的(00s ... 11s)数组转换为char字符串? 在我的代码中,我取长度为64的int数组,然后将数组除以多次,每次取8个等于8位的索引,然后从长度为8的数组的索引7开始,然后将索引的值乘以( 2 ^索引号,第一次应为7,然后是6.等)

但我收到一个例外。

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 8
    at testing.cipherText(testing.java:30)
    at testing.main(testing.java:8)

如果我的算法不正确,请告诉我

import java.util.*; 

public class testing { 

 public static void main(String [] args)
 {
     int [] array ={0,1,0,1,1,0,0,1,0,1,0,1,1,0,0,1,0,1,0,1,1,0,0,1,0,1,0,1,1,0,0,1,0,1,0,1,1,0,0,1,0,1,0,1,1,0,0,1,0,1,0,1,1,0,0,1,0,1,0,1,1,0,0,1};
     String c = cipherText (array);
     //System.out.print(i);
 }

 public static String cipherText (int [] array)
 {
     int decimal = 0;
     int [] intA = new int [8];
     int from = array.length;
     char x = 0;
     int dre;
     String s = null;
     for (int i = 0; i < array.length; i = i + 8)
     {
          from=from-8;
          intA=copyPartOfArray(array,from,8);
          for (int j=0;j<intA.length;j++)
          {
              dre = (int) Math.pow (2.0, (double)7-i);
              dre = dre * intA[i];
              decimal = decimal+dre;
              x =(char)decimal;
           }
           s=x+s;
        }         
        return s;
    }
    public static int [] copyPartOfArray (int [] a, int from, int to) // return a subArray 
    {
        int [] result=new int [to];
        System.arraycopy(a,from, result, 0, to); 
        return result;
    } 
}

无需创建新数组:

public static String cipherText(int[] array) {
    StringBuilder s = new StringBuilder();
    for (int i = 0; i < array.length / 8; i++) {
        byte dec = 0;
        for (int j = 0; j < 8; j++) {
            int pow = 1 << 7 - j; // == Math.pow(2, 7 - j)
            dec += array[i * 8 + j] * pow;
        }
        s.append((char) dec);
    }
    return s.toString();
}

好的,你说的是你的问题

dre = (int) Math.pow(2.0, (double) 7 - i);
dre=dre*intA[i];

我想你想要的

dre = (int) Math.pow(2.0, (double) 7 - j);
dre=dre*intA[j];

你也应该把你的字符串搞砸了

    String s = "";

代替

    String s = null;

您期望从阵列输出什么输出。 还要记住,java中的字符是16位unicode。

尽管如此,我认为你的算法还没有运行......会继续寻找

你可能想与...比较

public static String cipherText(int[] array) {
    byte[] bytes = new byte[array.length / 8];
    for (int i = 0; i < array.length; i += 8)
        bytes[i / 8] = (byte) ((array[i] << 7) + (array[i + 1] << 6) + (array[i + 2] << 5) + (array[i + 3] << 4) 
                + (array[i + 4] << 3) + (array[i + 5] << 2) + (array[i + 6] << 1) + array[i]);
    return new String(bytes, 0);
}

不使用pow<< ,只需标准parseByte

int [] array={0,1,0,1,1,0,0,1,
              0,1,0,1,1,0,0,1,
              0,1,0,1,1,0,0,1,
              0,1,0,1,1,0,0,1,
              0,1,0,1,1,0,0,1,
              0,1,0,1,1,0,0,1,
              0,1,0,1,1,0,0,1,
              0,1,0,1,1,0,0,1};
StringBuilder buffer = new StringBuilder();
assert array.length % 8 == 0;
for(int current:array) {
  buffer.append(current);
}   
int steps = array.length/8;
byte [] letters = new byte[steps];
for(int i=0; i<steps; i++) {
   letters[i] = Byte.parseByte(buffer.substring(i*8, i*8+8), 2); 
}   
String result = new String(letters);
System.err.println(result);

}

暂无
暂无

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

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