1

I am trying to convert an integer to a binary number using an integer array. The first conversion is toBinaryString where I get the proper conversion "11111111" then the next step to convert to an array. This is where it goes wrong and I think its the getChar line.

int x = 255;

string=(Integer.toBinaryString(x));

int[] array = new int[string.length()];

for (int i=0; i< string.length(); i++){
array[i] = string.getChar(i);

   Log.d("TAG", " Data " + array[1] "," + array[2] + "," + array[3]);

Log displays ( Data 0,0,0 ) the results I am looking for is ( Data 1,1,1 )

Here is the final code and it works.

        // NEW
int x = 128;

string=(Integer.toBinaryString(x));

int[] array = new int[string.length()];

for (int i=0; i < string.length(); i++) {
array[i] = Integer.parseInt(string.substring(i,i+1));
}

Log.d("TAG", "Data   " + array[0] + "" + array[1]+ "" + array[2] + "" + array[3]+  " " + array[4]+ "" + array[5] + "" + array[6] + "" + array[7]);
4
  • How do you call String.getChar(), dont see it available. I wonder how your app is not crashing when doing array[3] since I see that there would be indexes as [0,1&2] Commented Sep 5, 2013 at 22:29
  • 3
    There's no String.getChar(int) method. Did you mean String.charAt(int index)? Try posting code that compiles. Commented Sep 5, 2013 at 22:30
  • Code did compile but I had array[i] = string.getChar(i); commented out since there was errors. Array should be 0 through 7 so log should not crash the array[3]. Mattias, I get a Type mismatch: cannot convert from Integer[] to int[] on int digits[] = new Integer[Integer.SIZE]; Commented Sep 5, 2013 at 22:47
  • Thank you EVERYONE, I used code from each of your ideas and got it working. Thank you again.. Commented Sep 5, 2013 at 23:37

3 Answers 3

5
// Take your input integer
int x = 255;
// make an array of integers the size of Integers (in bits)
int[] digits = new Integer[Integer.SIZE];
// Iterate SIZE times through that array
for (int j = 0; j < Integer.SIZE; ++j) {
  // mask of the lowest bit and assign it to the next-to-last
  // Don't forget to subtract one to get indicies 0..(SIZE-1)
  digits[Integer.SIZE-j-1] = x & 0x1;
  // Shift off that bit moving the next bit into place
  x >>= 1;
}
Sign up to request clarification or add additional context in comments.

2 Comments

I was thinking of something similar, just using a boolean[] to represent the bit array and use one of the answers to this question. However, it is unclear from the OP if the resulting should array should have minimally size (array[0] is always 1, unless x == 0) or fixed size (array.length == Integer.SIZE (i.e. 32)).
I understand the array starts at 0 but I was only interested in 1,2,3 for log reference. Any ideas on why : int digits[] = new Integer[Integer.SIZE];gave errors.
2

how about :

array[i] = Integer.parseInt(string.substring(i,i+1));

Comments

2

First of all, an array starts from the index of 0 instead of 1, so change the following line of code:

Log.d("TAG", " Data " + array[1] "," + array[2] + "," + array[3]);

To:

Log.d("TAG", " Data " + array[0] "," + array[1] + "," + array[2]);

Next, the following line of code:

array[i] = string.getChar(i);

You are trying to get a character value to an Integer array, you may want to try the following instead and parse the value into an Integer (also, the "getChar" function does not exist):

array[i] = Integer.parseInt(String.valueOf(string.charAt(i)));

I hope I've helped.

P.S - Thanks to Hyrum Hammon for pointing out about the String.valueOf.

4 Comments

I'm pretty sure that'll have to be Integer.parseInt(String.valueOf(string.charAt(i)));
Not sure about getChar, there's no such method on String. Also, Integer.parseInt has a lower-case "p" (conforming with the Java naming conventions).
Hmm, it seems there's no parseInt(char), so that will throw a compiler error. You could make it into a string first, but Character.digit(string.charAt(i), 2) is probably faster and more straight to the point.
yeah, just checked. You do need String.valueOf

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.