I have to implement a recursive function that, given a decimal number lower than 1024, transforms it into binary, using as an element of storage an array of type char. And it has to show - > Example: 10 --- > 0000001010.
But the recursive function header must be: void conversion (int i, char [] array, int bytes).
Where n is the value to be converted to binary, arrayBin is the array of char initialized to 0 in each position, and nbits the number of bits of our result in binary.
public void conversion(int i, char [] array, int bytes) {
final int LIMITE = 1024;
if(LIMITE == i) {
throw new IllegalArgumentException("El numero debe ser menor a 1024");
}
if(0 == i) return;
int tmpN = i;
i /= 2;
//array[array.length-(++bytes)]= (tmpN % 2 == 0) ? '0' : '1';
conversion(i, array, bytes);
}
I found this code but I do not understand much the commented part of code, I do not know if it could be done in another way. For example recursively.
Greetings