1

I need to send an ArrayBuffer via chrome.sockets.udp plugin.

The data is sent via Android ionic app to an emulator that runs on a computer.

I need to send the following fields:

var arrayBuffer = new ArrayBuffer(20);
var dv = new DataView(arrayBuffer,0);

console.log("ID=0 assign");
dv.setUint16(0,0);
console.log("SIZE=12 assign");
dv.setUint16(2,12);
console.log("CRC=0 assign");
dv.setUint16(4,0);
console.log("MSGcount assign");
dv.setUint16(6,0);
console.log("AppVersion assign");
dv.setUint32(8,0);
console.log("Port assign");
dv.setUint32(12,7602);
for (var i = 0; i <= 19; i++) {
  console.log("byte number is " +i+ " "  +dv.getInt8(i));
}
dv.setUint32(16,20000);

When I run this code I get the following result:

byte number is 12 0
byte number is 13 0
byte number is 14 29
byte number is 15 -78

the server should get the next result:

byte number is 12 78
byte number is 13 29
byte number is 14 0
byte number is 15 0

How should I insert the numbers in dataView to get the bytes in the order the server expects them to be?

mplungjan
  • 169,008
  • 28
  • 173
  • 236
user2199630
  • 203
  • 1
  • 2
  • 12
  • Actually, the expected result for the server should have `byte number is 12 178`. Remember that the number was set as unsigned, so it should be retrieved as unsigned. – 4castle Jun 15 '16 at 16:06
  • i need it to be 78, as the server expects, how can i do that? – user2199630 Jun 15 '16 at 16:40
  • Either change the original number from `7602` to `7502`, or take the absolute value of the first byte on your server side. – 4castle Jun 15 '16 at 17:09

1 Answers1

1

You can reverse the endianness of the bytes set to the DataView by setting true to the third parameter of setUint32. This will use "little-endian" ordering, and have the result you expect:

dv.setUint32(12, 7602, true);

var arrayBuffer = new ArrayBuffer(20);
var dv = new DataView(arrayBuffer, 0);

dv.setUint32(12, 7602, true);

for (var i = 12; i <= 15; i++) {
  console.log("byte number is " + i + " " + dv.getInt8(i));
}

Note: the value is negative here because it's being accessed as a signed int8. If you access as an unsigned int8, you will see the "correct" underlying value.

4castle
  • 32,613
  • 11
  • 69
  • 106
  • Hi, thanks for the reply, but when i use `dv.getInt8(i)` i get the following results: `byte number is 12 178`, why is it not 78? – user2199630 Jun 15 '16 at 16:37
  • The unsigned binary form of 7602 with 32 bits is `00000000 00000000 00011101 10110010`. It's just displaying the values of each of those bytes as an unsigned number, namely `0 0 29 178` (use a decimal to binary converter to see for yourself). When you use little-endian ordering, it reverses the order of those bytes, so it will be `10110010 00011101 00000000 00000000` aka `178 29 0 0`. If you view the bytes as signed, it will transform the number using twos complement and use the first bit for representing positive/negative. – 4castle Jun 15 '16 at 16:54
  • `178` and `-78` have the same binary representation, but they are interpreted differently when you're using unsigned vs signed. – 4castle Jun 15 '16 at 17:01