1

I am given a string that can represent hex values and am told to convert it to such and store it in a string:

String str = new String("FF E7 C3 E7 FF");
String[] arrayStr = str.split(" ");

Then I need to convert each hex value to its binary equivalent:

String[] arrayBin = new String[arrayStr.length];
for (int i = 0; i < arrayStr.length; i++) {
        arrayBin[i] = Integer.toBinaryString(Integer.parseInt(arrayStr[i],16));
}

This is where I get lost. Is this binary string technically one dimensional or two dimensional? Basically, I need to convert it to a two dimensional array so that I can iterate through it in both x and y directions, to perform something like this:

for (int k = 0; k < arrayBin.length; k++) {
    for (int l = 0; l < arrayBin[k].length; l++) {
        if (arrayBin[k][l] == "1") {
            arrayBin[k][l] = "x";
        } else {
            arrayBin[k][l] = " ";               
        }
    }
}

At this point I get nothing but compiler errors, and despite all the articles online about multidimensional arrays, no search has given me something that works. I have tried things like:

String[][] arrayBin2 = new String[arrayBin.length][]
for (int m = 0; m < arrayBin.length; m++) {
    arrayBin2[m][] = arrayBin[m];
}

But that did not work. Any help or suggestions as to what I am doing wrong would be greatly appreciated.


Second attempt in response to @Eran 's suggestions:

public class Challenge3 {
    public static void main(String args[]) {
        String str = new String("FF E7 C3 E7 FF");
        String[] arrayStr = str.split(" ");
        String[] arrayBin = new String[arrayStr.length];
        for (int i = 0; i < arrayStr.length; i++) {
            arrayBin[i] = Integer.toBinaryString(Integer.parseInt(arrayStr[i],16));
        }

        for (int k = 0; k < arrayBin.length; k++) {
            StringBuilder sb = new StringBuilder(arrayBin[k].length());     
            for (int l = 0; l < arrayBin[k].length(); l++) {
                if (arrayBin[k].charAt(l) == 1) {
                    sb.append("x");
                } else {
                    sb.append(" ");            
                }
            }
            arrayBin[k] = sb.toString();
            System.out.println(arrayBin);
        }
    }
}

As a side note, the code is supposed to print out the following when done correctly:

xxxxxxxx
xxx  xxx
xx    xx
xxx  xxx
xxxxxxxx 
1
  • Using == to compare two Strings in Java is bad because both Strings need to point to the same object for that to be true. Use arrayBin[k][l].equals("1") Commented Apr 5, 2015 at 16:29

1 Answer 1

1

Your arrayBin is 1-dimentional.

If you want to iterate over the bits of each binary String, use the methods length() and charAt() of the String class :

for (int k = 0; k < arrayBin.length; k++) {
    StringBuilder sb = new StringBuilder(arrayBin[k].length());
    for (int l = 0; l < arrayBin[k].length(); l++) {
        if (arrayBin[k].charAt(l) == '1') {
            sb.append ('x');
        } else {
            sb.append (' ');            
        }
    }
    arrayBin[k] = sb.toString();
    System.out.println(arrayBin[k]);
}

Output :

xxxxxxxx
xxx  xxx
xx    xx
xxx  xxx
xxxxxxxx

Note that you can't change the String instances in your arrayBin array, since String is immutable. What you can do is construct a new String and replace each String in the arrayBin array with the new String.

Sign up to request clarification or add additional context in comments.

3 Comments

I tried to add your code in, and it compiles, but when I print it out all I get are blank lines, no actual content. I had to change the .equals("1") part to "== 1" because otherwise it wouldn't compile (not sure why not). I have edited and added the full code with your suggestions to my original post, under the dashed. thanks for the help.
@resu Note that in your updated code there are two problems - == 1 should be == '1' and you should print each String of the array separately (see modified code in my answer).
Thanks for the help @Eran , after seeing the answer I feel that I should totally re-name this post, since what I thought the problem was, and what the problem really was, were not the same.

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.