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]);
Bobby
  • 659
  • 2
  • 13
  • 26
  • 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] – Varun Sep 05 '13 at 22:29
  • 3
    There's no `String.getChar(int)` method. Did you mean `String.charAt(int index)`? Try posting code *that compiles*. – Mattias Buelens Sep 05 '13 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]; – Bobby Sep 05 '13 at 22:47
  • Thank you EVERYONE, I used code from each of your ideas and got it working. Thank you again.. – Bobby Sep 05 '13 at 23:37

3 Answers3

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;
}
caskey
  • 12,305
  • 2
  • 26
  • 27
  • I was thinking of something similar, just using a `boolean[]` to represent the bit array and use one of the answers to [this question](http://stackoverflow.com/q/8151435/1321716). 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)). – Mattias Buelens Sep 05 '13 at 22:42
  • 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. – Bobby Sep 05 '13 at 23:00
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.

MrByte
  • 1,083
  • 2
  • 11
  • 21
  • 1
    I'm pretty sure that'll have to be `Integer.parseInt(String.valueOf(string.charAt(i)));` – Hyrum Hammon Sep 05 '13 at 22:32
  • 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). – Mattias Buelens Sep 05 '13 at 22:33
  • 1
    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)`](http://docs.oracle.com/javase/7/docs/api/java/lang/Character.html#digit%28char,%20int%29) is probably faster and more straight to the point. – Mattias Buelens Sep 05 '13 at 22:37
  • 1
    yeah, just checked. You do need String.valueOf – Hyrum Hammon Sep 05 '13 at 22:37
2

how about :

array[i] = Integer.parseInt(string.substring(i,i+1));
Petros Koutsolampros
  • 2,790
  • 1
  • 14
  • 20