Reputation: 95
I am trying to convert an integer value to a boolean value. I want to represent my boolean value with X bits determined in advance.
for example :
1 = 01 (If i want to represent it with 2 bits)
1 = 0001 (If i want to represent it with 4 bits)
I don't know how impose the number of bits. Also that is my result when I convert 1 :
1 = 1
2 = 10
this is my method called DecimalToBinary. THKS
StringBuilder result = new StringBuilder();
if(no==0){
for(int i=0; i<variableArray.size(); i++){
result.append("0");
}
}else{
while(no>0){
result.append(no%2);
no = no/2;
}
}
return Integer.valueOf(result.reverse().toString());
no is a integer argument of my method
Upvotes: 0
Views: 191
Reputation: 4841
You could use Integer.toBinaryString()
. Check length of the result and prepend it with desirable number of zeroes.
Upvotes: 1