如何将 Byte []转换为 Byte [] ,反过来呢?

在不使用任何第三方库的情况下,如何将 byte[]转换为 Byte[],也将 Byte[]转换为 byte[]

有没有办法只用标准库就能快速完成呢?

115518 次浏览

Byte class is a wrapper for the primitive byte. This should do the work:

byte[] bytes = new byte[10];
Byte[] byteObjects = new Byte[bytes.length];


int i=0;
// Associating Byte array values with bytes. (byte[] to Byte[])
for(byte b: bytes)
byteObjects[i++] = b;  // Autoboxing.


....


int j=0;
// Unboxing Byte values. (Byte[] to byte[])
for(Byte b: byteObjects)
bytes[j++] = b.byteValue();

From byte[] to Byte[]:

    byte[] b = new byte[]{1,2};
Byte[] B = new Byte[b.length];
for (int i = 0; i < b.length; i++)
{
B[i] = Byte.valueOf(b[i]);
}

From Byte[] to byte[] (using our previously-defined B):

    byte[] b2 = new byte[B.length];
for (int i = 0; i < B.length; i++)
{
b2[i] = B[i];
}

You could use the toPrimitive method in the Apache Commons lang library ArrayUtils class, As suggested here - Java - Byte[] to byte[]

byte[] toPrimitives(Byte[] oBytes)
{


byte[] bytes = new byte[oBytes.length];
for(int i = 0; i < oBytes.length; i++){
bytes[i] = oBytes[i];
}
return bytes;


}

Inverse:

//byte[] to Byte[]
Byte[] toObjects(byte[] bytesPrim) {


Byte[] bytes = new Byte[bytesPrim.length];
int i = 0;
for (byte b : bytesPrim) bytes[i++] = b; //Autoboxing
return bytes;


}

Java 8 solution:

Byte[] toObjects(byte[] bytesPrim) {
Byte[] bytes = new Byte[bytesPrim.length];
Arrays.setAll(bytes, n -> bytesPrim[n]);
return bytes;
}

Unfortunately, you can't do this to convert from Byte[] to byte[]. Arrays has setAll for double[], int[], and long[], but not for other primitive types.

byte[] to Byte[] :

byte[] bytes = ...;
Byte[] byteObject = ArrayUtils.toObject(bytes);

Byte[] to byte[] :

Byte[] byteObject = new Byte[0];
byte[] bytes = ArrayUtils.toPrimitive(byteObject);

If someone preferes Stream API over ordinary loops.

private Byte[] toObjects(byte[] bytes) {
return IntStream.range(0, bytes.length)
.mapToObj(i -> bytes[i])
.toArray(Byte[]::new);
}

Step back. Look at the bigger picture. You're stuck converting byte[] to Byte[] or vice versa because of Java's strict type casing with something like this

List< Byte> or List<Byte[]>

Now you have byte[] and Byte[] and have to convert. This will help.

Keep all your byte[]s in a list like this: List<byte[]> instead of List< Byte> or List<Byte[]>. (byte is a primitive, byte[] is an object)

As you acquire bytes do this (networking socket example):

ArrayList<byte[]> compiledMessage = new ArrayList<byte[]>;
...
compiledMessage.add(packet.getData());

Then, when you want to put all your bytes in a single message, do this:

byte[] fromListOfNotByteArray (List<byte[]> list) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos;
try {
oos = new ObjectOutputStream(baos);
oos.writeObject(list);
} catch (IOException e) {
e.printStackTrace();
}
return baos.toByteArray();
}

That way, you can keep all your parts in List<byte[]> and your whole in byte[] without a bunch of crazy copy tasks in for loops with little baby byte[]s everywhere. ;)

Now you know -- teach others.