I have two byte arrays and I am wondering how I would go about adding one to the other or combining them to form a new byte array.
-
If you add them then how would you deal with overflow? – maerics Apr 16 '11 at 00:05
-
Where are you doing any addition? – Oliver Charlesworth Apr 16 '11 at 00:07
-
Bytes can be 0-255 so the sum of two bytes can overflow the range. (E.g. 255 + 1 = 256, overflow!) – maerics Apr 16 '11 at 00:14
-
1@maerics:Bytes are signed (-128 to 127). They overflow back into the negative range. – Mark Peters Apr 16 '11 at 00:18
-
Oh, right, I forgot that everything is signed in Java. But I suppose overflow is still overflow, right? – maerics Apr 16 '11 at 00:28
-
Using `ByteArrayOutputStream` is a nice way. See http://stackoverflow.com/a/9133993/1617737 – ban-geoengineering Oct 25 '16 at 18:04
7 Answers
You're just trying to concatenate the two byte
arrays?
byte[] one = getBytesForOne();
byte[] two = getBytesForTwo();
byte[] combined = new byte[one.length + two.length];
for (int i = 0; i < combined.length; ++i)
{
combined[i] = i < one.length ? one[i] : two[i - one.length];
}
Or you could use System.arraycopy
:
byte[] one = getBytesForOne();
byte[] two = getBytesForTwo();
byte[] combined = new byte[one.length + two.length];
System.arraycopy(one,0,combined,0 ,one.length);
System.arraycopy(two,0,combined,one.length,two.length);
Or you could just use a List
to do the work:
byte[] one = getBytesForOne();
byte[] two = getBytesForTwo();
List<Byte> list = new ArrayList<Byte>(Arrays.<Byte>asList(one));
list.addAll(Arrays.<Byte>asList(two));
byte[] combined = list.toArray(new byte[list.size()]);
Or you could simply use ByteBuffer
with the advantage of adding many arrays.
byte[] allByteArray = new byte[one.length + two.length + three.length];
ByteBuffer buff = ByteBuffer.wrap(allByteArray);
buff.put(one);
buff.put(two);
buff.put(three);
byte[] combined = buff.array();

- 22,034
- 5
- 72
- 84
-
9
-
2
-
And personally, I always use System.arraycopy, often with wrapper methods for the common case... actually I've long since written an `ArrayUtil.resize` method. – Lawrence Dol Apr 16 '11 at 00:50
-
1Arrays.asList() does not work with an array of primitive types. How can this *actually* be done? – Snackoverflow Aug 02 '18 at 12:02
-
2You cannot use `list.toArray(new byte[list.size()])` with primitive types. Instead it should be `Byte[] combined = list.toArray(new Byte[list.size()])` – Ilker Cat Sep 27 '18 at 10:20
-
`ByteBuffer#put(byte[])` will always use offset 0. Meaning that doing `buff.put(one);` followed by `buff.put(two);` will not append but overwrite. You would need to do `buff.put(one.length, two);` – Jasper Siepkes Jul 13 '23 at 13:19
You can do this by using Apace common lang package (org.apache.commons.lang.ArrayUtils
class ). You need to do the following
byte[] concatBytes = ArrayUtils.addAll(one,two);
-
1
-
2Your fear of overheads make me think you are troubled by your UI freezing whenever you perform this loop. Perhaps you should consider an `AsyncTask` (android), which is the only way of stopping a loop from interrupting your UI thread. – 1owk3y Jul 27 '15 at 13:14
I think it is best approach,
public static byte[] addAll(final byte[] array1, byte[] array2) {
byte[] joinedArray = Arrays.copyOf(array1, array1.length + array2.length);
System.arraycopy(array2, 0, joinedArray, array1.length, array2.length);
return joinedArray;
}

- 107
- 1
- 7
The simplest method (inline, assuming a
and b
are two given arrays):
byte[] c = (new String(a, cch) + new String(b, cch)).getBytes(cch);
This, of course, works with more than two summands and uses a concatenation charset, defined somewhere in your code:
static final java.nio.charset.Charset cch = java.nio.charset.StandardCharsets.ISO_8859_1;
Or, in more simple form, without this charset:
byte[] c = (new String(a, "l1") + new String(b, "l1")).getBytes("l1");
But you need to suppress UnsupportedEncodingException
which is unlikely to be thrown.
The fastest method:
public static byte[] concat(byte[] a, byte[] b) {
int lenA = a.length;
int lenB = b.length;
byte[] c = Arrays.copyOf(a, lenA + lenB);
System.arraycopy(b, 0, c, lenA, lenB);
return c;
}

- 3,498
- 3
- 12
- 33
String temp = passwordSalt;
byte[] byteSalt = temp.getBytes();
int start = 32;
for (int i = 0; i < byteData.length; i ++)
{
byteData[start + i] = byteSalt[i];
}
The problem with your code here is that the variable i that is being used to index the arrays is going past both the byteSalt array and the byteData array. So, Make sure that byteData is dimensioned to be at least the maximum length of the passwordSalt string plus 32. What will correct it is replacing the following line:
for (int i = 0; i < byteData.length; i ++)
with:
for (int i = 0; i < byteSalt.length; i ++)

- 3,687
- 1
- 32
- 45
I've used this code which works quite well just do appendData and either pass a single byte with an array, or two arrays to combine them :
protected byte[] appendData(byte firstObject,byte[] secondObject){
byte[] byteArray= {firstObject};
return appendData(byteArray,secondObject);
}
protected byte[] appendData(byte[] firstObject,byte secondByte){
byte[] byteArray= {secondByte};
return appendData(firstObject,byteArray);
}
protected byte[] appendData(byte[] firstObject,byte[] secondObject){
ByteArrayOutputStream outputStream = new ByteArrayOutputStream( );
try {
if (firstObject!=null && firstObject.length!=0)
outputStream.write(firstObject);
if (secondObject!=null && secondObject.length!=0)
outputStream.write(secondObject);
} catch (IOException e) {
e.printStackTrace();
}
return outputStream.toByteArray();
}

- 4,145
- 1
- 35
- 41
-
cool comment 7 years later @Farid, thanks for your input and for reviving the thread. – Jeremie D Jan 03 '22 at 15:54
Assuming your byteData
array is biger than 32 + byteSalt.length()
...you're going to it's length, not byteSalt.length
. You're trying to copy from beyond the array end.

- 3,297
- 3
- 25
- 37