简体   繁体   English

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

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

How to convert an array of (00s...11s), the binary representation of each character, to a string of char? 如何将每个字符的二进制表示形式的(00s ... 11s)数组转换为char字符串? In my code I take int array of length 64, then divide the array multiple times, each time I take 8 indexes equivalent to 8 bits, then start from index 7 from the array of length 8, then multiply the value of the index by (2^index number which should be 7 for the first time, then 6. etc.) 在我的代码中,我取长度为64的int数组,然后将数组除以多次,每次取8个等于8位的索引,然后从长度为8的数组的索引7开始,然后将索引的值乘以( 2 ^索引号,第一次应为7,然后是6.等)

But I receive an exception. 但我收到一个例外。

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

If my algorithm is not correct, please tell me 如果我的算法不正确,请告诉我

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

Without creating a new array: 无需创建新数组:

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

Ok, your problem is you are saying 好的,你说的是你的问题

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

I think you wanted 我想你想要的

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

Also you should initalise your string 你也应该把你的字符串搞砸了

    String s = "";

instead of 代替

    String s = null;

What output are you expecting, from the array. 您期望从阵列输出什么输出。 Remember also, chars in java are 16bit unicode. 还要记住,java中的字符是16位unicode。

Still, I don't think your algorithm works quite yet... Will keep looking 尽管如此,我认为你的算法还没有运行......会继续寻找

you might want to compare with ... 你可能想与...比较

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

Without using pow or << , just standard parseByte : 不使用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.

相关问题 如何编写函数字符串替换(char a,char b,string s),用b替换字符串s中每次出现的字符a - how can I write a function string replace (char a , char b, string s) that replace each occurrence of the character a in the string s by b 如何将包含二进制表示形式的字符串转换为int数组 - How to convert String containing binary representation into int array 如何将字符串的二进制表示形式转换回字符串? - How to convert a binary representation of a string back into a string? 如何将字节转换为其二进制字符串表示 - How to convert a byte to its binary string representation 如何在Java中将字符串的二进制表示转换为字节? - How to convert a binary representation of a string into byte in Java? 如何将ASCII值的字符串表示形式转换为字符 - How to convert string representation of an ASCII value to character 在另一个数组中以int数组的二进制表示形式存储1的数字 - Storing number of 1's in binary representation of an int array in another array 如何转换ArrayList <String> 字符数组,然后向后打印每个单词? - How to convert ArrayList<String> to char Array and then print each word backwards? 将二进制字符串转换为字符 - Convert binary string to char 如何打印字符串中第一个字符的二进制表示 - How do I print the binary representation of the first character in a string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM