开发者

Removing the first 16 bytes from a byte array

In Java, how do I take a byte[] array and remove the first 16 bytes from the array? I know I 开发者_开发问答might have to do this by copying the array into a new array. Any examples or help would be appreciated.


See Arrays class in the Java library:

Arrays.copyOfRange(byte[] original, int from, int to)

from is inclusive, whereas to is exclusive. Both are zero based indices, so to remove the first 16 bytes do

Arrays.copyOfRange(original, 16, original.length);


byte[] a;

...

if(a.length > 1) {
    byte[] newA = new byte[a.length-2];
    for(int i = 2; i < a.length; ++i)
        newA[i-2]=a[i];
}


void remove(byte[] b)
{
    for(i=16;i<b.length;i++)
    {
        a[i-16]=b[i];
        Process... arrays
    }
}


System.arraycopy() also can do that:

public static byte[] truncate(byte[] bytes, int srcPos, int newLength) {
    if (bytes.length < newLength) {
        return bytes;
    } else {
        byte[] truncated = new byte[newLength];
        System.arraycopy(bytes, srcPos, truncated, 0, newLength);
        return truncated;
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜