Encode and decode text into binary and vice versa

Viewed 38

I'm trying to convert text into binary and then decode it back to the original text.

When I encode my encoded binary bits are too small. At the decode side I got only the last character of my text string. My code is given below. Can anyone correct it, if there is any mistake? I want to decode the same text which I encode.

public static String enc(String s) {
///first layer of encoding

        String initializer="11111111";
        int ct=0;
        int a[]=new int[11111111];
        for (int i = 0; i <s.length(); i++){

            a[i]=(int)s.charAt(i);//charAAT retrun value in geven index
            ct++;
        }

        String res ="";
        int bin[] =new int[111];
        int idx=0;
        for(int i1=0; i1 < ct; i1++) {
            int temp =a[i1];
            for(int j = 0; j < ct; j++) bin[j]=0;
            idx = 0;

            while(temp>0) {
                  bin[idx++]=temp%2;
                  temp = temp/2;
            }
            String dig = "";
            String temps;
            for (int j = 0; j < 7; j++) {
                 temps = Integer.toString(bin[j]);

                 dig = dig.concat(temps);

            }
            String rev = "";
            for (int j =dig.length()-1; j>= 0; j--) {
                 char ca = dig.charAt(j);
                 rev = rev.concat(String.valueOf(ca));


            }


            res = rev.concat(rev);

        }
        res = initializer.concat(res);
        return res;
        }
}

When encoded, the output for text hi is 1111111111010011101001

The decode part returns only i as output:

String invalid = "Invalid Code";

        // create the same initial
        // string as in encode class
        //
        String initailizer = "11111111";
        Boolean flag = true;

        // run a loop of size 8
        for (int i = 0; i < 8; i++) {
            // check if the initial value is same
            if (initailizer.charAt(i) != res.charAt(i)) {
                flag = false;
                break;
            }
        }
        String data = "";
        for (int i = 8; i < res.length(); i++) {
            char temp = res.charAt(i);
            data = data.concat(String.valueOf(temp));
        }
        int asc[][] = new int[11101][8];
        int idx = -1;
        int idx2 = 0;
        for (int i = 0; i < data.length(); i++) {
            if (i%7==0) {
                idx++;
                idx2 =0;
                char temp = data.charAt(i);
                asc[idx][idx2] = temp-'0';
                idx2++;

            } else
                {
                 char temp = data.charAt(i);
                 asc[idx][idx2] = temp -'0';
                 idx2++;

            }
        }

        int num[] = new int[11111];
        int nidx = 0;
        int temp = 0;
        int ct = 0;
        for (int i = 0; i < idx; i++) {
            ct = 0;
            temp = 0;
            for (int j=6; j>=0;j--) {

                int temp1 = (int) pow(2, ct);
                temp += (asc[i][j]*temp1);
                ct++;
            }
            num[nidx++] = temp;
        }
        String rv = "";
        char ch;
        for (int i =0; i<nidx; i++) {
            ch = (char) num[i];
            rv = rv.concat(String.valueOf(ch));


        }
        if (data.length()%7 == 0 && flag == true) {
            //return rv;

        }
        return rv;


    }
0 Answers
Related