Created
January 26, 2014 01:28
-
-
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.
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 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