Skip to content

Instantly share code, notes, and snippets.

@SIRHAMY
Created February 6, 2014 04:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SIRHAMY/8838367 to your computer and use it in GitHub Desktop.
Save SIRHAMY/8838367 to your computer and use it in GitHub Desktop.
Converts an integer to a String of binary in ASCII characters
public static String itostrb(int binary)
{
String result = "0";
int scalar = 1;
int placeHolder = 1;
while(binary>0){
if((binary - scalar*2)>=0){
scalar*=2;
placeHolder++;
}else{
binary -= scalar;
scalar = 1;
result="1";
break;
}
}
for(int i = placeHolder - 1; i > 0; i--){
scalar = 1;
String add = "0";
for(int j = 0; j < i - 1; j++){
scalar*=2;
}
if((binary - scalar)>=0){
binary-=scalar;
add = "1";
}
result += add;
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment