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.
我有两个字节数组,我想知道如何将一个字节数组添加到另一个字节数组中,或者将它们组合成一个新的字节数组。
6 个解决方案
#1
89
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:
或者你也可以使用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()]);
#2
18
You can do this by using Apace common lang package (org.apache.commons.lang.ArrayUtils
class ). You need to do the following
您可以通过使用Apace公共lang包(org.apache.common .lang. arrayutils类)来实现这一点。您需要执行以下操作
byte[] concatBytes = ArrayUtils.addAll(one,two);
#3
4
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;
}
#4
2
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.
假设您的byteData数组比32 + byteSalt.length()…它是长度,不是bytesalt。length。你试图从数组的末端复制。
#5
2
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:
这里代码的问题是用于索引数组的变量i同时经过byteSalt数组和byteData数组。因此,请确保byteData的维度至少是passwordSalt字符串的最大长度加上32。什么可以纠正它是取代了以下一行:
for (int i = 0; i < byteData.length; i ++)
with:
:
for (int i = 0; i < byteSalt.length; i ++)
#6
1
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 :
我用了这段代码,它很好地完成了appendData,或者用一个数组传递一个字节,或者用两个数组组合它们:
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
89
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:
或者你也可以使用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()]);
#2
18
You can do this by using Apace common lang package (org.apache.commons.lang.ArrayUtils
class ). You need to do the following
您可以通过使用Apace公共lang包(org.apache.common .lang. arrayutils类)来实现这一点。您需要执行以下操作
byte[] concatBytes = ArrayUtils.addAll(one,two);
#3
4
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;
}
#4
2
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.
假设您的byteData数组比32 + byteSalt.length()…它是长度,不是bytesalt。length。你试图从数组的末端复制。
#5
2
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:
这里代码的问题是用于索引数组的变量i同时经过byteSalt数组和byteData数组。因此,请确保byteData的维度至少是passwordSalt字符串的最大长度加上32。什么可以纠正它是取代了以下一行:
for (int i = 0; i < byteData.length; i ++)
with:
:
for (int i = 0; i < byteSalt.length; i ++)
#6
1
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 :
我用了这段代码,它很好地完成了appendData,或者用一个数组传递一个字节,或者用两个数组组合它们:
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();
}