0

Can anyone give me a util method that will give me first 16bit from a bytearray in Android/Java?

Code:

 //Decode API response using Base64 decoder
            byte[] decodedResult = Base64.decode(apiResponse, Base64.DEFAULT);

            //get first 16 bit from decodedResult
            byte[] first16Bit = 

DUMMY Example in Flex language:

//Storing First 16 bits of databytearray to ivByteArray
                var ivByteArray:ByteArray = new ByteArray();
                ivByteArray.writeBytes(dataByteArray,0,16);
//Storing from 16 bits to length of databytearray to encByteArray 
                var encByteArray:ByteArray = new ByteArray();
                encByteArray.writeBytes(dataByteArray,16);
Pooja Singh
  • 149
  • 2
  • 10

1 Answers1

1

You can use copyOfRange from Java.

Ex.,

byte[] first16Bit = Arrays.copyOfRange(dataByteArray, 0, 16)

Update I didn't noticed that in the question user has been asked about bit instead of byte, anyway, if you want first 16 bit you can use following.

byte[] first16Bit = Arrays.copyOfRange(dataByteArray, 0, 2)
Gunaseelan
  • 14,415
  • 11
  • 80
  • 128
  • Are you sure that this is the same thing which is done in flex which is written in my question? – Pooja Singh Feb 22 '20 at 07:38
  • but this is returning the first **128** bits (16 **bytes**)! wasn't question about **16 bits** ?!?!? – user85421 Feb 22 '20 at 08:35
  • Yes the first two bytes would be `byte [] first2bytes = .....,0, 2);`. Again i'm amazed that an OP does not make excuses for causing confusion. – blackapps Feb 22 '20 at 09:19
  • @PoojaSingh, I hope so, anyway you can try it and check whether this is same thing or not. – Gunaseelan Feb 22 '20 at 11:46
  • Can you please look at this: https://stackoverflow.com/questions/60370961/what-to-pass-in-cipher-dofinal-in-android-java – Pooja Singh Feb 24 '20 at 07:07
  • Hi, can you please solve? https://stackoverflow.com/questions/60429082/how-can-i-observe-api-call-based-on-user-submit-button-and-at-same-time-getvalue – Pooja Singh Feb 27 '20 at 10:31