Skip to content

Instantly share code, notes, and snippets.

@SIRHAMY
Created January 26, 2014 01:28
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/8626697 to your computer and use it in GitHub Desktop.
Save SIRHAMY/8626697 to your computer and use it in GitHub Desktop.
Converts a string of binary in ASCII characters to decimal in the form of the integer primitive data type.
public static int strbtoi(String binary)
{
int result = 0;
for(int i = 0; i<binary.length() ; i++){
if(binary.charAt(i)==49){
if(i==binary.length()-1){
result += 1;
}else{
int scalar = 2;
for(int j = i + 1; j < binary.length()-1;j++){
scalar*=2;
}
result+=scalar;
}
}
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment