0

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

1
  • To report a code defect (that's what you are doing) the pattern is: This is the input, this is the expected outcome/output, this is the actual outcome/output. You have told us what you expect but not what you currently get. Edit your question to give us that extra information to make your question a good one that give you practice for what will be expected of you every time you ask someone for help with your code. Commented Oct 29, 2016 at 21:12

2 Answers 2

1

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)));
}
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.