T
T
t38c3j2016-10-17 13:29:08
Arduino
t38c3j, 2016-10-17 13:29:08

How to parse binary data in C?

Hello. Raised a web server on the ESP12 to return statics and web sockets for data exchange.
The question is, on the client side in javascript, I form binary data and send them via a web socket

var arrayBuffer = new ArrayBuffer(20);
var dataView = new DataView(arrayBuffer);
dataView.setUint8(0, 1);
dataView.setUint8(1, indexElement);

How can I parse this on the server side?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Rsa97, 2016-10-17
@t38c3j

As a rule, you define a structure corresponding to the data, then create a variable with the type of this structure, read the data into it, and simply access the fields of the structure.

#pragma pack(push,1)
typedef _websocketPacket {
  unsigned char one; // здесь ваша 1
  unsigned char indexElement;
  unsigned char rest[18];
} wsPacket;
#pragma pack(pop)
wsPacket myData;

A
abcd0x00, 2016-10-18
@abcd0x00

The question is, on the client side in javascript, I form binary data and send them over a web socket

You must serialize them into some specific (or standardized) format like json or xml and then parse such data on the receiving side.
Even sish data from one machine to another is not transferred directly, because the same sish program can have different sizes of the same types on different computers.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question