Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to merge 2 binarybyte array in java [duplicate]

Tags:

java

arrays

I have 2 byte array like this:

byte a[i]=1;
byte b[i]=0;

I want to merge it so that the output result becomes "10". I try to use arraycopy and make new output

byte[] output=a.length+b.length;

but it still doesn't work like my expectation. Anybody knows how to solve it?

like image 905
user3698011 Avatar asked Dec 29 '25 10:12

user3698011


1 Answers

Try this.

byte[] first = getFirstBytes();
byte[] second = getSecondBytes();

List<Byte> listOfBytes = new ArrayList<Byte>(Arrays.<Byte>asList(first));
listOfBytes.addAll(Arrays.<Byte>asList(second));

byte[] combinedByte = listOfBytes.toArray(new byte[listOfBytes.size()]);
like image 141
ashokramcse Avatar answered Dec 31 '25 01:12

ashokramcse



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!