One way to generate a binary byte array in custom protocol format

Source: Internet
Author: User

 

During network communication, one party needs to transmit data to the other. The data can be values, one statement, and an array.

In C #, the socket class is used for communication. The send (byte [] B) parameter is in the byte array format, that is, when sending

You must convert the data you want to send to a byte array before sending the data.

To get data in the byte array.

So how does the receiving end know the significance of the byte values in the received byte array? Of course you can know, because

Protocol!

A Data Protocol roughly stipulates the following: data format, data Header Format, data content, and data tail representation.

Partially. In a word, it is a description of the data content and organization method.

The data sender works on the security protocol to compile the data. In this byte array, 0-3 bytes are the data header, and 4-5 bytes are the data.

Length, 6-26 bytes for content, and 27-28 for verification code.

NMEA-0183 protocol data: $ gpgga, 134658.00, 5106.9792, N, 11402.3003, W, 1.0, 1048.47, 16.27, M,-, M, 08, AAAA * 60

The recipient already knows how to parse the data, So how should the sender organize the data?

How can we add existing data to some specified locations in the byte array according to protocol rules?

Different Languages have their own implementation methods. Taking C # as an example, you can use the generic byte list method:

1. Define a conversion buffer.

List<byte> buffer = new List<byte>();

2. Convert the data into a byte array and store it in the buffer zone. multiple types of data can be saved.

float HeightD = 1.2616f;buffer.AddRange(BitConverter.GetBytes(HeightD));

3. Convert the data in the buffer to a byte array

byte[] message = new byte[length];buffer.CopyTo(0, message, 0, length)

At this point, the byte array sent by message is obtained.

I have not tried to define a struct object, assign values to the struct object attribute, and then retrieve the byte array from the object's memory address.

 

This is just one of my own methods. I urge the passing experts to specify the true meaning of data compilation.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.