1

I need to create a byte array with deferent data types in it, For example: i will have data that will contain Byte (0-100), Byte(0-10), Two bytes(-30-+100), Bool(0/1), Byte, Two Bytes(0-300).

The client will receive the bytes array (using Buffer) and will get the data from the hax that Buffer creates by using offsets. So i need to always retain the number of bytes as in the API specs i'm getting from the client.

Example:

Battery.protorype.onSubscribe = function(maxValuesSize, updateValueCallback) {
    var bytes = Array(6);
    bytes[0] = 50;
    bytes[1] = 2;
    bytes[2] = -20;
    bytes[3] = true;
    bytes[4] = 32;
    bytes[5] = 290;
    updateValueCallback(new Buffer(bytes));

Will return: 0x3202ec012022

This of course is not good because of two things:

  1. -20 is ec? and 290 is 22? (what happen to the first byte? 290 dec is 0x122 and this is two bytes)

  2. Event if that was correct (if the numbers were contained in a single byte), I need to keep the sizes to maintain offsets and this does not maintain offsets as all the numbers over here are of size of one byte.

Does any one knows how to solve this issue?

Erez
  • 1,933
  • 5
  • 29
  • 56

2 Answers2

2

You should do the treatment yourself, I would use of a customized class. Like :

// Use of an array, where you gonna hold the data using describing structure
this.array = [];

// Store values along with the size in Byte
push(value, size) {
  this.array.push({
    size,
    value,
  });
}

// Turn the array into a byte array
getByteArray() {
  // Create a byte array
  return this.array.reduce((tmp, {
    size,
    value,
  }) => {
    // Here you makes multiple insertion in Buffer depending on the size you have

    // For example if you have the value 0 with a size of 4.
    // You make 4 push on the buffer
    tmp.push(...);

    return tmp;
  }, new Buffer());
}

EDIT: more explanation

You have to create a class that will handle the data storage and treatment.

When we are dealing with a data, we store it associated with it's size in Byte.

for example the number 12 in 3 Byte, we gonna store { value: 12, size: 3 }.

When we gonna have to generate the Byte array, we gonna use the size we did store in order to push the correct amount of Byte into the Buffer array.

for example the number 12 in 3 Byte.

We gonna store in the Buffer 0, 0 and 12.


To be clear :

BEFORE

array.push(12);

new Buffer(array);

The buffer read the array, takes 12 and convert it into Byte, so 0x0C

You end up with Buffer = [ 0x0C ]


NOW

array.push({ value: 12, size: 3 });

array.reduce( ...

 // Because we have a size of 3 byte, we push 3 Byte in the buffer

 buffer.push(0);
 buffer.push(0);
 buffer.push(12);

..., new Buffer());

You end up with Buffer = [ 0x00, 0x00, 0x0C ]

Orelsanpls
  • 22,456
  • 6
  • 42
  • 69
  • Can you please explain some more about this as it can't make it work... 10x – Erez Sep 12 '17 at 09:48
  • 10x, just one thing, all of your example where numbers that where less then a Byte, what about a number that is larger then one Byte, like 290? what will happen then? why would that work if i'm pushing zeros in the bytes before it? why would it device the number between the bytes? 290 is 0x122, so what will happen there? – Erez Sep 12 '17 at 10:43
  • For 290, you gotta make binary calculation to define the bytes you have to send. For example, if you want the most significant byte of 0x0122, you will do : 0x0122 & 0xFF00 >> 16. The result will be 0x01. – Orelsanpls Sep 12 '17 at 11:14
  • OK, then i was right, This answer was not what i was looking for, now i understand... 10x anyway. I was looking for somthing like bitUtils for Java but thank you very much – Erez Sep 12 '17 at 11:30
  • 1
    @Erez I've found [this](https://github.com/node-modules/byte) idk if it can help :) – Orelsanpls Sep 12 '17 at 11:53
0

Two bytes(-30-+100) // There is no issue with this value. -30 is es in 8-bit two's complement signed integer so this can be stored in 1 byte.

Two Bytes(0-300) // can be stored in 2 bytes. Convert number to bits for exp. using (300).toString(2) and store into 2 bytes.

Sandeep Sharma
  • 1,855
  • 3
  • 19
  • 34
  • I know, but as i said, the client is deciding the specs for the offsets and they decided that it should be 2 bytes for their own reasons... 10x – Erez Sep 12 '17 at 09:33
  • so allocate 2 bytes for each value and make/compose a buffer and send to the client and ultimately he would parse values using offset 2. – Sandeep Sharma Sep 12 '17 at 09:48