如何从字节数组复制字节部分?

时间:2019-06-07 08:04:20

标签: java arrays byte bitstring

我拿了一个字符串,例如“ hello”并将其设置为字节数组(str.getBytes()),现在我想选择一些位,例如5,17,26,...(最多4个字节/ 32位)由8位字节组成的字节数组并将位选择转换为整数,但是我不知道如何...什么都没有为我工作,我不想使用特殊的库。

我尝试过移位,但是我似乎不太了解它的工作原理……尽管这可能是最好的方法

public int[] calcInts(String str) { // str e.g. "hello" or "prhfetg"

byte[] bytes = str.getBytes() // make byte array
selection = 9 // variable - can be greater or smaller than 8 (1 byte)
numOfParts = (str.length*8)/selection  
int[] arr = new int[numOfParts]

for (int i=0; i<numOfParts; i++) { // calculate integers 

// HELP section --> do something with byte array to extract selection

int[i] = (int)... // integer from (max 32bit) selection 
}

return arr;
}

0 个答案:

没有答案