分析:现有字符串"a",转换byte后假设取位为:00110001,现去掉第一个0为0110001,补位加0为01100010.
现要求将一组去掉第一位的串将第一位重新添加回来,即分析的逆过程。代码如下
byte [] aryBytes = temp.getBytes();
StringBuffer sbtmp = new StringBuffer();
StringBuffer sb = new StringBuffer();
System.out.println("content内容:"+temp);
for(int i=0;i<aryBytes.length;i++){
sbtmp.append((byte)((aryBytes[i] >> 7) & 0x1));
sbtmp.append((byte)((aryBytes[i] >> 6) & 0x1));
sbtmp.append((byte)((aryBytes[i] >> 5) & 0x1));
sbtmp.append((byte)((aryBytes[i] >> 4) & 0x1));
sbtmp.append((byte)((aryBytes[i] >> 3) & 0x1));
sbtmp.append((byte)((aryBytes[i] >> 2) & 0x1));
sbtmp.append((byte)((aryBytes[i] >> 1) & 0x1));
sbtmp.append((byte)((aryBytes[i] >> 0) & 0x1));
}
System.out.println("原始二进制串:"+sbtmp);
int length = sbtmp.length();
for(int i=0;i<length/7;i++){
sb.append("0"+sbtmp.substring(i*7,(i+1)*7));
}
System.out.println("补0后二进制串:"+sb);
byte [] bytes = new byte[sb.length()/8];
for(int i=0;i<sb.length()/8;i++){
bytes[i] = Byte.parseByte(sb.substring(i*8, (i+1)*8),2);
}
new String(bytes);