How to convert byte[] to Byte[] and the other way around?
105,823
Solution 1
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();
Solution 2
byte[] to Byte[] :
byte[] bytes = ...;
Byte[] byteObject = ArrayUtils.toObject(bytes);
Byte[] to byte[] :
Byte[] byteObject = new Byte[0];
byte[] bytes = ArrayUtils.toPrimitive(byteObject);
Solution 3
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.
Solution 4
You could use the toPrimitive method in the Apache Commons lang library ArrayUtils class, As suggested here - Java - Byte[] to byte[]
Solution 5
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;
}
Author by
user926958
Updated on April 09, 2020Comments
-
user926958 about 4 years
How to convert
byte[]
toByte[]
and alsoByte[]
tobyte[]
, in the case of not using any 3rd party library?Is there a way to do it fast just using the standard library?
-
Dunes over 11 yearsWould definitely recommend
Byte.valueOf(b)
overnew Byte(b)
. I would be surprised if the Byte class didn't cache every single value for a byte. -
Edwin Dalorzo over 11 yearsI believe that it might be better to use
Byte.valueOf(byte)
. JavaDocs say that this method should generally be used in preference to the constructorByte(byte)
, as this method is likely to yield significantly better space and time performance since all byte values are cached. -
Code-Apprentice over 11 yearsWith autoboxing, you can simply do
byteObjects[i++] = b;
-
BalusC over 11 yearsI'd use
new Byte[bytes.length];
instead ofnew Byte[10];
to keep it sensible. -
user504342 over 10 yearsThis answer isn't correct. The i++ and j++ in the for-each loop don't do anything useful. You need to use the basic for-loop: for (int i=0; i<bytes.length;i++) { byteObjects[i] = b[i]; }. Same goes for unboxing.
-
Juvanis over 10 years@user504342 have you ever debugged my code? with my code i and j goes from 0 to 10 and handles the job.
-
user504342 over 10 years@Juvanis aha, I see that you use two "counters" in the for-loop: one to iterate over the primitive byte array and/or Byte object array, and another one for incrementing the index of the primitive byte array and/or Byte object array. To me, this is bad practice. If this code is maintained by others, and someone would modify the counter "i" from 0 to another value, or would change the i++ into ++i, the indexes no longer match. I am not advocating anyone should modify the code in the first place, but it's a common pitfall for beginners. My suggestion keeps the indexes of both arrays in sync.
-
ARRG over 10 yearsThis is actually the best answer, at least for anyone for whom adding a dependency to commons-lang isn't an issue.
-
mvorisek over 6 yearsWhy this is not the best answer? Any performace reason or just answered later?
-
mvorisek over 6 yearsBecause the ArrayUtils are not a part of Java, but Apache lib.
-
ElectRocnic almost 4 yearsand the other way round?
-
mkalmo almost 4 yearsI think you can't get byte[] from Stream. Stream api has mapToInt(), mapToDouble() and mapToLong() methods but not mapToByte().
-
Alex8752 almost 4 yearsI agree with @mvorisek, this should be considered the best answer. Just add org.apache.commons.lang3.ArrayUtils
-
Liscare about 3 yearsBest answer for me. Simple code and no third-party library.
-
Binita Bharati about 3 yearsUnfortunately,
ArrayUtils
is not part of standard Java packages. -
Steve Bosman over 2 yearshow does this have upvotes when it is code that doesn't work?
-
Frédéric Nobre about 2 yearsapache commons lang is a great package that should be part of any java project.