标签:
将正常字符串转二进制字符串
/**
* 将正常字符串转二进制字符串
* @param s
* @return
*/
private static String StringToBinary(String s) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < s.length(); i++) {
//得到单个字符
char at = s.charAt(i);
//将字符转为二进制
int binary = Integer.parseInt(Integer.toBinaryString(at));
sb.append(binary+" ");
}
return sb.toString();
}
将二进制字符串转为正常字符串
/**
* 将二进制字符串转为正常字符串
* @param sBin
* @return
*/
private static String BinaryToString(String sBin) {
StringBuffer sb = new StringBuffer();
//将sBin分割成String数组
String[] split = sBin.split(" ");
for (int i = 0; i < split.length; i++) {
//将split元素转为int型的数值
int sp = Integer.parseInt(Integer.valueOf(split[i],2).toString());
//将int型的数值强转为char
sb.append((char)sp);
}
return sb.toString();
}
标签:
原文地址:http://blog.csdn.net/tuu_zed/article/details/46591767