[英]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?在 Java 中,如何获取 byte[] 数组并从数组中删除前 16 个字节? 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 :请参阅Java 库中的
Arrays
类:
Arrays.copyOfRange(byte[] original, int from, int to)
from
is inclusive, whereas to
is exclusive. from
是包含性的,而to
是排斥性的。 Both are zero based indices, so to remove the first 16 bytes do两者都是基于零的索引,因此要删除前 16 个字节
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: System.arraycopy()
也可以这样做:
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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.