Miguel Pimentel
Miguel Pimentel

Reputation: 5

Converting each binary number to 8 characters String

I have been trying to convert these numbers to 8 character strings.

I have some trouble because the zeros on left are ignored. For example.

The Expected result: 00111111 result that i got: 111111

Here's my code:

    String s = new String("xlactz3Ja8Z/qep6niE");

        System.out.println("String: " + s);

        byte[] b = Base64.getDecoder().decode(s);
        String res = "";

        for(byte a : b)
        {
            int num = 255 & a; //Tool for set only 8 bits for time
            res = res + " " + Integer.toString(num, 2);
        }

        System.out.println();
        System.out.println(res);

        res = res.trim();

        transform(res, 0);

    }

    private static void transform(String s, int i)
    {
        String aux = new String();
        String newString = new String ();

        char[] v = s.toCharArray();

        for(int i1 = i; i1 < s.length(); ++i1)
        {
            if(v[i1] != ' ')
            {
                aux = aux + v[i1];
            }
            else
            {
                if(aux.length() == 8)
                {
                    newString = newString + aux + " ";
                    aux = "";
                }
                else
                {
                    for(int j= 0; j < 8 - aux.length(); ++j)
                        aux = "0" + aux;

                    newString = newString + aux + " ";

                    aux = "";                   
                }
            }
        }

        System.out.println(newString);
    }
}

Expected output for the code:

11000110 01010110 10011100 10110111 00111101 11001001 01101011 11000110 01111111 10101001 11101010 01111010 10011110 00100001

Upvotes: 0

Views: 258

Answers (2)

janos
janos

Reputation: 124656

This loop doesn't work well for you:

for(int j= 0; j < 8 - aux.length(); ++j)
    aux = "0" + aux;

Because in each iteration step, the length of aux changes, therefore the limit changes. In fact the limit gets smaller, so if 2 or more characters need to be prepended, the loop will exit earlier than you need.

Rewrite like this:

for (int j = aux.length(); j < 8; ++j)
    aux = "0" + aux;

This has the added benefit that aux.length() is only called once. It's in fact key for the current functioning of your algorithm.

Another problem is that the last value in s will not get appended. To fix that, you need to add this exact same loop after the main loop in your code. Like this:

    for (int i1 = i; i1 < s.length(); ++i1) {
        if (v[i1] != ' ') {
            aux = aux + v[i1];
        } else {
            if (aux.length() == 8) {
                newString = newString + aux + " ";
                aux = "";
            } else {
                for (int j = aux.length(); j < 8; ++j)
                    aux = "0" + aux;

                newString += aux + " ";

                aux = "";
            }
        }
    }

    for (int j = aux.length(); j < 8; ++j)
        aux = "0" + aux;

But this code is truly awful. It would be better to implement the binary string generation using bit shifting. Consider this:

private static String toBinaryString(int num, int width) {
    char[] bits = new char[width];
    for (int i = width - 1; i >= 0; i--) {
        bits[i] = (char)('0' + (num & 1));
        num >>= 1;
    }
    return new String(bits);
}

You can call this method with the number you want to convert as the first parameter, and 8 as the second. With the help of this function, you can drop the transform function, and change the loop in the middle of your first function like this:

for (byte a : b) {
    int num = 255 & a;
    res += " " + toBinaryString(num, 8);
}

Upvotes: 0

Mandeep Rajpal
Mandeep Rajpal

Reputation: 1777

Maybe, instead of using transform function you could do something like this in your loop -

for (byte a : b) {
    int num = 255 & a; // Tool for set only 8 bits for time
    res = res + " " + String.format("%08d", Integer.parseInt(Integer.toString(num, 2)));
}

Upvotes: 1

Related Questions