Java - Bin2Text - Second Draft |
||
![]() Cpt SAJChurchey public static String Bin2Text(String str){
if(str == null || str.trim().length() == 0)
return "";
str = str.trim();
if(str.matches("[^01\\s]")){
/*
User needs to be warned that their input is invalid
Method must terminate
*/
return ""; //Until proper exception can be written
}
StringTokenizer tokenizedStr = new StringTokenizer(str);
StringBuffer text = new StringBuffer();
while(tokenizedStr.hasMoreTokens()){
String binaryString = tokenizedStr.nextToken();
int bytelen = binaryString.length();
if(bytelen > 16){
//User needs to be told input is not valid
return ""; //Until exception can be written.
}
int intValue = bin2decimal(binaryString);
char textEquivalent = (char) intValue;
text.append(textEquivalent);
}//end while
return text.toString();
}
private static int bin2decimal(String binary){
int decimal = 0;
for(int i = binary.length() - 1;i >= 0;--i){
int asciiValue = binary.charAt(i) - '0';
decimal += Math.pow(2,i) * asciiValue;
}//end for
return decimal;
}
Cpt SAJChurchey
C/O of Editorial OSI Staff edit0r OSI Feedback Representative Replies:
|
||
| CyberArmy::Forum v0.6 Generated In 0.05179 seconds |