简体   繁体   中英

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? 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.)

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.

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 :

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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