I have a problem: I use byte[] to store data, but I have to connect several byte[] togather, I know Arrays.addAll(Arrays.asList(Byte[])) will do the thing, but how to convert byte[] to Byte[], and how to revert Byte[] to byte[]?
Asked
Active
Viewed 1,253 times
-2
-
1Please show us what you've tried so far, by [edit]ing your question. Also please convey any research that you have done. – Jonny Henly Aug 05 '16 at 03:07
-
1And also tell us what the results were. For example, "I tried this but it gave me this compile error:
" or "I tried this and passed in [1, 2] but I got out [0]." – Robert Columbia Aug 05 '16 at 03:09 -
If I were you, I'd just do it by brute force--declare your own `byte[]` whose length is the total length, and use loops to copy the data. Java doesn't always do a good job with arrays of primitive types, and some newer Java 8 features that could help you with some primitive types aren't defined for `byte`. – ajb Aug 05 '16 at 03:28
1 Answers
0
Convert Byte to byte
public class convertByteTobyte {
public static void main(String[] args) {
Byte[] Byte = new Byte[50];
System.out.println("Byte length is:" + Byte.length);
byte[] b = new byte[Byte.length];
for (int i = 0; i < Byte.length; i++) {
b[i] = Byte[i];
}
System.out.println("byte length is:" + b.length);
}
}
Convert byte to Byte
public class ConvertbyteToByte {
public static void main(String[] args) {
byte[] b = new byte[50];
System.out.println("byte length is:" + b.length);
Byte[] Byte = new Byte[b.length];
for (int i = 0; i < b.length; i++) {
Byte[i] = b[i];
}
System.out.println("Byte length is:" + Byte.length);
}
}

Suman Behara
- 150
- 9