有没有更好的方法从二进制字符串中获取bit []
例如
假设我想要从索引= 3到长度(len = 5)的位
BinaryString = 100 11000 000000010000111110000001
预期结果= 11000左
这是我到目前为止所做的。
方法1
public void getBits1(){
int idx = 3;
int len = 5;
String binary = new BigInteger("98010F81", 16).toString(2);
char[] bits = binary.toCharArray();
String result = "";
//check here: to make sure len is not out of bounds
if(len + idx > binary.length())
return; //error
for(int i=0; i<len; i++){
result = result + bits[idx];
idx++;
}
//original
System.out.println(binary);
//result
System.out.println(result);
}
方法2
public void getBits2(){
int idx = 3;
int len = 5;
String binary = new BigInteger("98010F81", 16).toString(2);
String result = binary.substring(idx, len+idx);
//original
System.out.println(binary);
//result
System.out.println(result);
}
答案 0 :(得分:0)
我认为value.substring(int,int)没问题。