0

This is the code I used:

byte[] arr = {127,12,-74,85,-3,0,-112,104,.........}; //size: 1024              
ByteBuffer wrapped = ByteBuffer.wrap(arr); 
short num = wrapped.getShort(); 

Taken from here

In my app for fun:

public void(byte[] bytes) {
    byte[] arr = bytes;
    ByteBuffer wrapped = ByteBuffer.wrap(arr); // big-endian by default
    short num = wrapped.getShort(); // 1

    Log.i("bytes",""+num);}

I have a byte array of size 1024-bytes[1024] in which, value of each byte is between 127 to -128. When I run the above code, I get values of num as -27356, 30647, 28033, -29854... for each byte array of size 1024. I have many byte arrays which I pass in the method.

I did not understand the code and the value of num. What is happening here?

Suppose:

byte[] bytes={127,12,-74,85,-3,0,-112,104,.........} size: 1024
  1. What does bytebuffer.wrap() do?
  2. If I put bytes like this: bytebuffer.wrap(bytes), what will happen?
  3. What is getshort() method? What does it do?
  4. What does num value represent?
  5. Is num representing single integer value of entire byte array of size 1024?
Community
  • 1
  • 1
user6007737
  • 49
  • 3
  • 7

2 Answers2

0

1)What does bytebuffer.wrap()do?

See javadoc: Wraps a byte array into a buffer. The new buffer will be backed by the given byte array; that is, modifications to the buffer will cause the array to be modified and vice versa. The new buffer's capacity and limit will be array.length, its position will be zero, and its mark will be undefined. Its backing array will be the given array, and its array offset will be zero.

2)If i put bytes like this: bytebuffer.wrap(bytes),what will happen?

You get a ByteBuffer backed by the byte[]. See #1.

3)What is getshort() method?What does it do?

See javadoc: Reads the next two bytes at this buffer's current position, composing them into a short value according to the current byte order, and then increments the position by two.

4)What does "num" value represent?

The signed value of two bytes read from the buffer. See #3.

5)Is "num" representing single integer value of entire byte array of size 1024?

No, it represent the value of two bytes. See #3 and #4.


You might also want to learn about 2's complement:
What is “2's Complement”? (StackOverflow)
Two's_complement (Wikipedia)

Community
  • 1
  • 1
Andreas
  • 154,647
  • 11
  • 152
  • 247
0

1) What does bytebuffer.wrap() do?

ByteBuffer.wrap takes an existing byte[] and creates a ByteBuffer from it exposing methods that correspond to buffer relation actions such as getShort().

2) If i put bytes like this: bytebuffer.wrap(bytes), what will happen?

You will fill the ByteBuffer with the contents of bytes instead of arr, arguably since arr is assigned to bytes just previously this will be an okay action. Although taking a copy of it is a good idea if you intended to use bytes as an original value later but that isn't present in this code.

3) What is getshort() method? What does it do?

The buffer contains a constant stream of bytes and a current location. When you getShort() you read two bytes from the current position and put them into the short structure. As present in the comments below your question

If two byte values are hex 0x95,0x24, decimal -107,36, then short is hex 0x9524, decimal -27356

These are appended as shown 0x9524 and then read out as if it was a short in structure, not two bytes. This removes one of the sign bits and treats it as a data bit which gives you a large negative value if the first value is negative.

4) What does "num" value represent?

That's hard to answer without saying the obvious. It represents the first two bytes composed as one short of data. In your first example you use

byte[] arr = { 0x00, 0x01 };

Which when appended is the short 0x0001 giving you the value 1. Commonly this "num" variable you have created is used like this in Network I/O Packet Structures and it usually represents the number of bytes in the packet after the first short or the packet type such as "Game Data" or similarily.

byte[] arr={127,12,-74,85,-3,0,-112,104,.........}

In this case it will read the first to bytes 127 and 12. These correspond to 0x7F and 0x0C which will give "num" = 0x7F0C = 32524. When you call this method with different byte arrays only the first two numbers are accounted for in num.

5) Is "num" representing single integer value of entire byte array of size 1024?

No, it represents only the first two bytes as shown in previous questions. The size of the array as long as it is bigger than 2 (sizeof(short) = 2 on normal systems) doesn't matter.

Joachim Olsson
  • 316
  • 1
  • 8
  • if byte[] arr={127,12,-74,85,-3,0,-112,104,.........} size:1024 please can u explain q.4 in respect to this array not in respect to first example – user6007737 Mar 17 '16 at 07:15
  • Num reads the first 2 bytes, in your byte array "arr" arr[0] is 127 – Joachim Olsson Mar 17 '16 at 12:55
  • The comment above submitted wrong, sorry. Num reads the first 2 bytes, in your byte array "arr" arr[0] is 127 = 0x7F arr[1] is 12 = 0x0C When num is read as short it will read and represent the first two bytes, in big endian arrays this is 0x7F0C which gives you the value (32524), in little endian arrays it reverses the reading order and gives you 0x0C7F which is 3199. I have edited the answer. – Joachim Olsson Mar 17 '16 at 13:01