-1

I'm developing an app based on Samsung Chord SDK. I need to send the video's current position in the sendDataToAll(), which accepts data in byte[][]. My problem is that when I try to send the current position (which is an int) type-casted into byte, I'm getting negative value (in byte) as returned. And when I try to convert the negative value in to int in the OnDataRecived(), it's still the same negative value. How do I solve this issue?

Sending code:

//for sending the message 
int currPos = mVideoView.getCurrentPosition();
logView.append("Duration sent= " + currPos);
//Integer resume = -3;

Byte msgByte = new Byte("-3");
byte [] [] pay = new byte [1] [2];
pay[0] [0] = msgByte;

Byte msgByte2 = new Byte((byte) currPos);
logView.append("Duration sent= " + msgByte2);
pay[0] [1] = msgByte2;
mChordchannel.sendDataToAll("Integer", pay);

// im sending -3 so that im checking that the user pressed the resume .
Receiving code:

 //on receiving 
else if(intRec == -3) {
    Byte pos = rec[0] [1];
    int posn;
    posn = pos.intValue();
    logView.append("Duration received= " + posn);
    mVideoView.seekTo(posn);
    mVideoView.start();
}  
tomrozb
  • 25,773
  • 31
  • 101
  • 122
user3559583
  • 33
  • 1
  • 7
  • Byte had a range of -128 to 127. If you have an integer with a value greater than 127 then the high-order bits will be truncated and you may get a negative value. – Hot Licks May 05 '14 at 03:47
  • @Hot Licks : thanks. You are correct. I need to convert a integer value say like 5 digit number to byte and then that byte to integer. Can u please explain me how to do this. – user3559583 May 05 '14 at 04:15
  • You can't store a greater range than the aforementioned -128 to 127 in a single Byte. What you want, I believe, is an array of Bytes, in which you can store the full value of an integer for later retrieval. – Silvio Mayolo May 05 '14 at 04:17
  • Actually im sending the current position of the video in message (byte array) and based on the position received i need to resume the video from that position.. the send message function takes the message in byte[][]. so im sending the current position (int value) in that array by type casting it in byte. nd then while receiving im again type casting the byte to int , to seek the video based on the position. – user3559583 May 05 '14 at 04:22
  • possible duplicate of [Convert integer into byte array (Java)](http://stackoverflow.com/questions/1936857/convert-integer-into-byte-array-java) – Viktor Seifert May 05 '14 at 09:55
  • 1
    To answer your question, we'd need to know how the Chord SDK data are encoded. There a plenty of ways (in fact:infinitely many) to encode an integer value in a number of bytes. Also, I am a bit suspicious if you really need an array of array of bytes. – Ingo May 05 '14 at 13:00

1 Answers1

1

I don't know anything about the Samsung Chord SDK, but you can't fit (most) ints in a byte. An int is 4 bytes wide.

To create a payload compatible with your current code, that sends all 4 bytes:

byte[][] payload = { {
    -3,
    (byte)(currPos >> 24),
    (byte)(currPos >> 16),
    (byte)(currPos >>  8),
    (byte)(currPos >>  0),
} };
mChordchannel.sendDataToAll("Integer", payload);

To receive:

int position = new java.math.BigInteger(
    Arrays.copyOfRange(rec[0], 1, 5)).intValue();

P.S. This is not pretty code! It is tolerable for basic ints, but if you later find you need to transmit more complicated data structures you will need a better way. Some ideas, in increasing order of sophistication:

  • Use data streams (wrap a DataOutputStream around a ByteArrayOutputStream; write values; when done writing, close the DataOutputStream and call toByteArray() on the ByteArrayOutputStream to get the payload).
  • Use serialization (wrap an ObjectOutputStream around a ByteArrayOutputStream; write values and/or objects; finish as above).
  • Use a JSON-encoder or XML-encoder. (E.g., encode the data as a String, then call getBytes("UTF-8") on the String and send that.)
Boann
  • 48,794
  • 16
  • 117
  • 146