Created
February 6, 2014 04:24
-
-
Save SIRHAMY/8838367 to your computer and use it in GitHub Desktop.
Converts an integer to a String of binary in ASCII characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode 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