java arrays bytearray byte

java - split string arduino delimiter



Dividir una matriz de bytes (3)

Puedes usar Arrays.copyOfRange() para eso.

¿Es posible obtener bytes específicos de una matriz de bytes en java?

Tengo una matriz de bytes:

byte[] abc = new byte[512];

y quiero tener 3 matrices de bytes diferentes de esta matriz.

  1. byte 0-127
  2. byte 128-255
  3. byte256-511.

Intenté abc.read(byte[], offset,length) pero funciona solo si doy offset como 0, para cualquier otro valor lanza una excepción IndexOutOfbounds .

¿Qué estoy haciendo mal?


También puede utilizar los búferes de bytes como vistas en la parte superior de la matriz original.


Arrays.copyOfRange() se introduce en Java 1.6. Si tiene una versión anterior, está utilizando internamente System.arraycopy(...) . Así es como se implementa:

public static <U> U[] copyOfRange(U[] original, int from, int to) { Class<? extends U[]> newType = (Class<? extends U[]>) original.getClass(); int newLength = to - from; if (newLength < 0) { throw new IllegalArgumentException(from + " > " + to); } U[] copy = ((Object) newType == (Object)Object[].class) ? (U[]) new Object[newLength] : (U[]) Array.newInstance(newType.getComponentType(), newLength); System.arraycopy(original, from, copy, 0, Math.min(original.length - from, newLength)); return copy; }