写这个小时候我主要考虑了以下几点:
1、作为工具类静态思路方法比较好点Java类库中util中就有很多是静态工具
2、原没有充分利用到Java类库有冗余自己编写代码越多bug几率越大!
3、异常处理是健壮代码不可少输入堆0、1很容易出错而且万分隔符分得不恰当呢?
4、在大量串连接时String创建了大量无用中间String其开销是指数增长d俄而用StringBuffer其开销是线性增长
5、用for/each循环代替for这个代码只是要遍历和下标无关用for/each更好点
备注:StringTokenizer类可以实现串分析但是StringTokenizer是出于兼容性原因而被保留遗留类建议所有寻求此功能人使用 String split思路方法或 java.util.regex 包中国自学编程网www.zxbc.cn
public StrBinaryTurn {
//将串转换成 2进制串以空格相隔
public String toBinary(String str){
char strChar = str.toCharArray;
String result = \"\";
for( i = 0; i < strChar.length; i){
result Integer.toBinaryString(strChar) + \" \";
}
result;
}
//将 2进制串转换成Unicode串
public String toStr(String binStr){
String tempStr = StrToStrArray(binStr);
char tempChar = char[tempStr.length];
for( i = 0; i < tempStr.length; i){
tempChar = toChar(tempStr);
}
String.valueOf(tempChar);
}
//将 2进制串转换为char
private char toChar(String binStr){
temp = binStrToIntArray(binStr);
sum = 0;
for( i = 0; i < temp.length; i){
sum temp[temp.length - 1 - i] << i;
}
(char)sum;
}
//将 2进制串转换成串以空格相隔
private String StrToStrArray(String str){
str.split(\" \");
}
//将 2进制串转换成
private binStrToIntArray(String binStr){
char temp = binStr.toCharArray;
result = [temp.length];
for( i = 0; i < temp.length; i){
result = temp - 48;
}
result;
}
public void (String args){
StrBinaryTurn cTob = StrBinaryTurn;
.out.prln(cTob.toBinary(\"橘子好吃!aaa\"));
.out.prln;
.out.prln(cTob.toBinary(\"999111\"));
.out.prln;
.out.prln(cTob.toBinary(\"What a nice day!\"));
.out.prln;
.out.prln(cTob.toBinary(\"^@^ - -! ()★\"));
.out.prln;
.out.prln;
.out.prln(cTob.toStr(\"110101001011000 101101101010000 \" +
\"1111111100001100 101100101111101 \" +
\"101010000000011 1111111100000001 \" + [Page]
\"1100001 1100001 1100001 \"));
.out.prln(cTob.toStr(\"111001 111001 111001 \" +
\"110001 110001 110001\"));
.out.prln(cTob.toStr(\"1010111 1101000 1100001 \" +
\"1110100 100000 1100001 100000 1101110 \" +
\"1101001 1100011 1100101 100000 1100100 \" +
\"1100001 1111001 100001 \"));
.out.prln(cTob.toStr(\"1011110 1000000 1011110 \" +
\"100000 101101 100000 101101 100001 100000 \" +
\"1111111100001000 1111111100001001 10011000000101 \"));
}
}
输出结果:
110101001011000 101101101010000 1111111100001100 101100101111101 101010000000011 1111111100000001 1100001 1100001 1100001
111001 111001 111001 110001 110001 110001
1010111 1101000 1100001 1110100 100000 1100001 100000 1101110 1101001 1100011 1100101 100000 1100100 1100001 1111001 100001
1011110 1000000 1011110 100000 101101 100000 101101 100001 100000 1111111100001000 1111111100001001 10011000000101
橘子好吃!aaa
999111
What a nice day!
^@^ - -! ()★
最新评论