你的意思是把字符串中的字符二进制字串连成整个字符串的BinaryString?
解决方案 »
- UDP DatagramPacket同时不间断向路由上一层2台主机发包,其中一台只收到一次数据。
- java正则表达式能解决下面问题吗?
- java如何调用c语言编译器 去编译c程序!
- 为什么输出是Hello?而不是Hello World?
- 高分求关于调入页面的算法问题。。
- 100分 求解linux下运行RSA解密算法的问题
- 关于Word的痕迹保留问题,请各位大侠出招!
- intellJ idea 3.0.5怎么才能支持assert?
- 问一个基本数据类型!
- 这个构造函数new String(byte[],int,int,int)怎麽用?谢谢!
- 請問誰有jdk1.4的類庫參考的電子書?
- 关于java连接数据库的问题!
2、对每一个byte,转换成0/1序列。 // 自己写方法实现,注意Java中的byte是有符号的。
3、把0/1序列依次合并到一起。
byte[] res = new byte[8];
if (b<0) {
b=(byte)-b;
res[0]=1;
}
else res[0]=0;
res[7] = b;
// Converting b to 0/1 sequence
for (int i = 7; i > 0; i--) {
if (res[i]>1) {
res[i-1] += res[i]/2;
res[i] %= 2;
}
}
// Copies the res array into a string
StringBuffer sb = new StringBuffer();
for (int j = 0; j < 8; j++) {
sb.append(res[j]);
}
return sb.toString();
}
* This method converts byte value into 8-bit binary sequence.
* @param byte the byte value to be converted
* @return byte[] a 8-element byte array representing the binary sequence
* */
static byte[] get01Seq(byte b){
byte[] res = new byte[8];
for (int i = 0; i < 8; i++) {
res[i] = (byte)(((1<<(7-i)&b)==0)?0:1);
}
return res;
}转换出来的是byte[],8个元素,其中就是0/1的序列。
String s = String.valueOf(Integer.toBinaryString(i)); //int to binary
System.out.println(s);
String ss = "11";
int ii = Integer.parseInt(ss,2); //binary to int,parseInt中2代表2进制,可使用2,8,10,16不同参数
System.out.println(ii);