93

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?

informatik01
  • 16,038
  • 10
  • 74
  • 104
user926958
  • 9,355
  • 7
  • 28
  • 33
  • 5
    10 questions, ONE accepted? No good – Alfabravo Oct 17 '12 at 22:33
  • Why do you need/have a `Byte[]`? Doesn't seem like a good idea... either use `byte[]` or `List`. – Dunes Oct 17 '12 at 22:38
  • Might be useful if the values can be null, though probably not very efficient (ie. wastes any space-benefit of storing bytes, because of all the object references)? – DNA Oct 17 '12 at 22:40
  • But you can't convert a `Byte[]` to a `byte[]` if you have null references... – Dunes Oct 17 '12 at 22:41
  • @BalusC ok, 1 of 7. Haven't improved a lot! – Alfabravo Oct 17 '12 at 22:43
  • @Dunes One reason to have Byte[] is that it becomes an intermediate form of trying to call toArray on a List. In this case, writing your own loop seems to be the easiest way. – Ben Oct 25 '13 at 18:45

9 Answers9

86

byte[] to Byte[] :

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

Byte[] to byte[] :

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

Note: ArrayUtils are not a part of Java, but Apache lib

Aaron Kurtzhals
  • 2,036
  • 3
  • 17
  • 21
LaCrampe
  • 905
  • 6
  • 5
61

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();
mike
  • 4,929
  • 4
  • 40
  • 80
Juvanis
  • 25,802
  • 5
  • 69
  • 87
  • 4
    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. – Dunes Oct 17 '12 at 22:34
  • 2
    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. – Edwin Dalorzo Oct 17 '12 at 22:34
  • 10
    With autoboxing, you can simply do `byteObjects[i++] = b;` – Code-Apprentice Oct 17 '12 at 22:34
  • 5
    I'd use `new Byte[bytes.length];` instead of `new Byte[10];` to keep it sensible. – BalusC Oct 17 '12 at 22:37
  • 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 – user504342 Dec 08 '13 at 02:07
  • @user504342 have you ever debugged my code? with my code i and j goes from 0 to 10 and handles the job. – Juvanis Dec 08 '13 at 08:43
  • 3
    @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. – user504342 Dec 08 '13 at 13:29
22

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.

ajb
  • 31,309
  • 3
  • 58
  • 84
16

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

Community
  • 1
  • 1
Giannis
  • 5,286
  • 15
  • 58
  • 113
  • This is actually the best answer, at least for anyone for whom adding a dependency to commons-lang isn't an issue. – ARRG Mar 13 '14 at 14:03
8
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;

}
jacktrades
  • 7,224
  • 13
  • 56
  • 83
5

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];
    }
DNA
  • 42,007
  • 12
  • 107
  • 146
3

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);
}
mkalmo
  • 185
  • 1
  • 5
0

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.

David Urry
  • 807
  • 1
  • 8
  • 15
0

Since Java 8:

byte[] bytes = new bytes[byteObject.length];
IntStream.range(0, byteObject.length).forEach(x -> bytes [x] = byteObject[x]);
dani77
  • 199
  • 2
  • 5
  • 26