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;

}
Share:
105,823
user926958
Author by

user926958

Updated on April 09, 2020

Comments

  • user926958
    user926958 about 4 years

    How to convert byte[] to Byte[] and also Byte[] to byte[], in the case of not using any 3rd party library?

    Is there a way to do it fast just using the standard library?

  • Dunes
    Dunes over 11 years
    Would definitely recommend Byte.valueOf(b) over new Byte(b). I would be surprised if the Byte class didn't cache every single value for a byte.
  • Edwin Dalorzo
    Edwin Dalorzo over 11 years
    I believe that it might be better to use Byte.valueOf(byte). JavaDocs say that this method should generally be used in preference to the constructor Byte(byte), as this method is likely to yield significantly better space and time performance since all byte values are cached.
  • Code-Apprentice
    Code-Apprentice over 11 years
    With autoboxing, you can simply do byteObjects[i++] = b;
  • BalusC
    BalusC over 11 years
    I'd use new Byte[bytes.length]; instead of new Byte[10]; to keep it sensible.
  • user504342
    user504342 over 10 years
    This 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
    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
    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
    ARRG over 10 years
    This is actually the best answer, at least for anyone for whom adding a dependency to commons-lang isn't an issue.
  • mvorisek
    mvorisek over 6 years
    Why this is not the best answer? Any performace reason or just answered later?
  • mvorisek
    mvorisek over 6 years
    Because the ArrayUtils are not a part of Java, but Apache lib.
  • ElectRocnic
    ElectRocnic almost 4 years
    and the other way round?
  • mkalmo
    mkalmo almost 4 years
    I think you can't get byte[] from Stream. Stream api has mapToInt(), mapToDouble() and mapToLong() methods but not mapToByte().
  • Alex8752
    Alex8752 almost 4 years
    I agree with @mvorisek, this should be considered the best answer. Just add org.apache.commons.lang3.ArrayUtils
  • Liscare
    Liscare about 3 years
    Best answer for me. Simple code and no third-party library.
  • Binita Bharati
    Binita Bharati about 3 years
    Unfortunately, ArrayUtils is not part of standard Java packages.
  • Steve Bosman
    Steve Bosman over 2 years
    how does this have upvotes when it is code that doesn't work?
  • Frédéric Nobre
    Frédéric Nobre about 2 years
    apache commons lang is a great package that should be part of any java project.