How to combine two byte arrays

I have two byte arrays, and I wonder how I will add one to the other or combine them to form a new byte array.

+56
java
Apr 16 2018-11-11T00:
source share
7 answers

Are you just trying to combine 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 can 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 can just use List to do the job:

 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 can just use ByteBuffer with the advantage of adding multiple 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(); 
+97
Apr 16 '11 at 12:30 a.m.
source share

You can do this using the Apace common lang package ( org.apache.commons.lang.ArrayUtils class). You need to do the following

 byte[] concatBytes = ArrayUtils.addAll(one,two); 
+22
Apr 13 '13 at 6:05
source share

I think this is the 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; } 
+4
Jun 06 '17 at 7:16
source share

Assuming your byteData array byteData larger than 32 + byteSalt.length() ... you are going to length it, not byteSalt.length . You are trying to copy because of the end of the array.

+2
Apr 16 '11 at 0:15
source share
 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 i variable, which is used to index arrays, goes past the byteSalt array and the byteData array. So, make sure that byteData has a size of at least the maximum passwordSalt line length plus 32. What will fix it, it replaces the following line:

 for (int i = 0; i < byteData.length; i ++) 

from:

 for (int i = 0; i < byteSalt.length; i ++) 
+2
Apr 16 2018-11-11T00:
source share

I used this code that works well, just adds appendData and either passes one 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(); } 
+1
Jul 10 '14 at 20:48
source share

The simplest method (built-in, assuming that 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 terms and uses the character set defined somewhere in your code:

 static final java.nio.charset.Charset cch = java.nio.charset.StandardCharsets.ISO_8859_1; 

Or, in a simpler form, without this encoding:

 byte[] c = (new String(a, "l1") + new String(b, "l1")).getBytes("l1"); 

But you need to suppress the 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; } 
+1
Nov 28 '18 at 20:14
source share



All Articles