A binary serialization method

By employing a compile-time computation method based on XML description, combined with binary serialization using bitmasks and shift operations, the problems of high code complexity and low performance in existing technologies are solved. This achieves efficient data transmission and a simplified serialization process, making it suitable for binary data transmission in the military field.

CN117492763BActive Publication Date: 2026-06-1910TH RES INST OF CETC

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
10TH RES INST OF CETC
Filing Date
2023-11-01
Publication Date
2026-06-19

Smart Images

  • Figure CN117492763B_ABST
    Figure CN117492763B_ABST
Patent Text Reader

Abstract

This invention provides a binary serialization method, comprising: S1: defining an XML format interface based on field types; S2: constructing a binary serialization algorithm and a deserialization algorithm according to the XML format interface; S3: retrieving data fields from a cache, reading data fields, and implementing data transmission based on the constructed binary serialization and deserialization algorithms according to the XML format interface. The serialization method of this invention performs field type determination and field position calculation at compile time, saving computational overhead during program runtime. Furthermore, based on the XML format structure, it modularizes both endianness-based and endianness-based serialization and deserialization logic, reducing technical requirements for personnel and improving coding efficiency.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of communication technology, and in particular to a binary serialization method. Background Technology

[0002] Serialization is the process of converting an object's state information into a form that can be stored or transmitted. During serialization, an object writes its current state to temporary or persistent storage, and the object can be recreated by reading from or deserializing its state from storage. In the military field, the communication mechanisms between various hardware and software differ from those in current internet applications. To ensure high utilization of storage space and high efficiency in data transmission, military hardware and software communicate using binary bitstreams, converting objects into bitstreams through binary serialization.

[0003] Throughout the history of computer development, due to differences in processor hardware architecture, two storage modes exist: big-endian and little-endian. These two storage modes correspond to different bitstream structures. When defining structures, programmers will define different structures based on endianness, resulting in different field orders within the structures. This is especially true when bit fields span multiple bytes, where the field order can vary significantly. Therefore, programmers need a deep understanding of the definition and storage order of bytes and bit fields in both endianness modes to correctly define structures.

[0004] There are three existing binary serialization methods. One method involves coders defining a structure based on the interface file and the actual serial number. The workflow is as follows: Figure 5 As shown. For fixed-length data, a structure with the corresponding endianness can be applied directly, allowing for direct addressing without requiring coders to write endianness conversion and serialization processes, resulting in fast processing speed. However, this approach involves high coupling between the interface logic and business logic, and demands a high level of technical expertise from the coders. Coders need a deep understanding of endianness and big-endian storage methods in computers; otherwise, errors are easily made. Especially when bit fields span multiple bytes, the order of structure fields becomes difficult to determine, making the correct structure definition process extremely challenging. For variable-length data, direct addressing is not possible, and coders still need to write endianness conversion and serialization processes. For example... Figure 6 As shown, another approach involves branching based on the data field type during program runtime, with coders manually implementing different serialization methods on different branches. This method is relatively simple and straightforward for very small-scale software development; however, in large-scale software development, this approach results in bloated and redundant code structure, unclear architectural hierarchy, maintainability and scalability issues, and excessive branching leads to poor processing performance, making it unsuitable for application scenarios such as high-speed data transmission. Figure 7As shown, another approach involves high-level abstraction of data fields, extracting common components as a base class. This base class provides a serialization interface, and different types of data fields are treated as subclasses. These subclasses implement the base class's serialization interface, and during program runtime, the serialization method of the subclasses is automatically called through object polymorphism. This approach offers a clear architectural hierarchy and high maintainability, extensibility, and reusability; however, in data-intensive scenarios, the accumulated time cost of polymorphic processing leads to low efficiency. Furthermore, creating a class instance for each data field wastes storage space. Summary of the Invention

[0005] To address the aforementioned problems, this invention provides a binary serialization method that implements binary serialization based on XML description, utilizing compile-time computation, and combining dynamic and static methods.

[0006] This invention provides a binary serialization method, the specific technical solution of which is as follows:

[0007] S1: Defines an XML format interface based on field type;

[0008] S2: Construct binary serialization and deserialization algorithms based on the XML format interface;

[0009] S3: Retrieves data fields from the cache, reads data fields, and implements data transmission based on the XML format interface and the constructed binary serialization and deserialization algorithms.

[0010] Furthermore, the defined XML format interface is as follows:

[0011] Based on the ICD interface definition, each data field is defined sequentially as its corresponding XML node.

[0012] Furthermore, the binary serialization and deserialization algorithms are constructed using cross-byte bit order processing based on bit masks and shift operations.

[0013] Furthermore, the binary serialization and deserialization algorithms include little-endian binary serialization and deserialization algorithms, as well as big-endian binary serialization and deserialization algorithms.

[0014] Furthermore, the little-endian binary serialization algorithm is specifically executed as follows:

[0015] Read the field value from the cache, get the input number of bits BitNum, and set the bit start position BitStartPos;

[0016] Calculate the number of bytes (ByteNum) based on the input bit length.

[0017] If it is four bytes of data, calculate the value of the corresponding data bit of each byte from the least significant bit to the most significant bit, update the bit start position BitStartPos, and perform a shift operation to obtain the value of the corresponding data bit of each byte in sequence;

[0018] If the data is multi-byte, calculate the value of each byte sequentially starting from the least significant byte, perform shift operations, update the bit start position BitStartPos, and process to obtain the value of each byte;

[0019] If it is a single byte of data, update the bit start position BitStartPos and perform a shift operation;

[0020] If it is two-byte data, calculate the lower octet and the higher octet separately, update the bit start position BitStartPos, and perform a shift operation to obtain the lower octet and the higher octet values ​​in sequence.

[0021] Furthermore, the specific execution logic of the little-endian binary deserialization algorithm is as follows:

[0022] Input buffer, get the number of input bits BitNum, and set the bit start position BitStartPos;

[0023] The number of bytes, ByteNum, is calculated at compile time based on the number of bits input.

[0024] Set a byte mask array and determine the number of bytes;

[0025] If it is four-byte data, calculate the value of the corresponding data bit of each byte, update the bit start position BitStartPos, calculate the byte start position ByteIndex and the bit start position within the byte BitIndex, read the value of the data field in the cache, and perform little-endian sequence calculation based on the byte mask array, and update the value of the corresponding data bit of each byte with the value of the calculation result in turn;

[0026] If the data is multi-byte, calculate the value of each byte sequentially starting from the least significant byte, update the bit start position BitStartPos sequentially, and perform little-endian sequence calculation based on the byte mask array to update the value of each byte.

[0027] If it is single-byte data, the little-endian sequence is calculated directly based on the byte mask array, and the result value is recorded and output.

[0028] If the data is two bytes, the lower octet and the higher octet are read, the bit start position BitStartPos is updated, and the little-endian sequence is calculated based on the byte mask array to update the lower octet and the higher octet in turn.

[0029] Furthermore, the little-endian sequence calculation process is as follows:

[0030] Get the current byte start position ByteIndex and the current byte bit start position BitIndex;

[0031] Perform a bitwise AND operation between the current byte value and the value of the BitIndex bit in the low-order byte mask array, and then perform a right shift operation to obtain the low octet value; perform a bitwise AND operation between the current byte value and the value of the BitIndex bit in the high-order byte mask array, and then perform a left shift operation to obtain the high octet value;

[0032] Assign the lower octet to the cached ByteIndex byte, and assign the higher octet to the cached ByteIndex+1 byte.

[0033] Furthermore, the big-endian binary serialization algorithm is executed according to the following logic:

[0034] Read the field value from the cache, get the input number of bits BitNum, input it into the cache Buffer, and set the bit start position BitStartPos;

[0035] Calculate the number of bytes (ByteNum) based on the input bit width, set the byte mask array, and determine the number of bytes;

[0036] If it is four bytes of data, calculate the value of the corresponding data bit of each byte from the high bit to the low bit, update the bit start position BitStartPos, and perform shift operation on the value of the corresponding data bit of each byte in turn to obtain the value of the corresponding data bit of each byte in turn.

[0037] If the data is multi-byte, calculate the value of each byte sequentially starting from the most significant byte, perform shift operations, update the bit start position BitStartPos, and perform shift operations sequentially based on the byte mask array to update the value of all bytes.

[0038] If it is a single byte of data, update the bit start position BitStartPos, and perform a shift operation based on the byte mask array to update the byte value;

[0039] If it is two-byte data, calculate the high octet and the low octet separately, update the bit start position BitStartPos, and perform a shift operation based on the byte mask array to obtain the value of the high octet and the low octet in sequence.

[0040] Furthermore, the specific execution logic of the big-endian binary deserialization algorithm is as follows:

[0041] Input buffer, get the number of input bits BitNum, and set the bit start position BitStartPos;

[0042] The number of bytes, ByteNum, is calculated at compile time based on the number of bits input.

[0043] Set a byte mask array and determine the number of bytes;

[0044] If it is four-byte data, calculate the value of the corresponding data bit of each byte from the high bit to the low bit, update the bit start position BitStartPos, calculate the byte start position and the byte inner bit start position, read the value of the data field in the cache, and perform big endian sequence calculation based on the byte mask array, and update the value of the corresponding data bit of each byte with the value of the calculation result in turn;

[0045] If the data is multi-byte, calculate the value of each byte sequentially starting from the least significant byte, update the bit start position BitStartPos sequentially, and perform big-endian sequence calculation based on the mask array to update the value of each byte.

[0046] If it is single-byte data, perform big-endian sequence calculation directly based on the mask array, and record the result value for output;

[0047] If the data is two bytes, read the lower octet and the higher octet, update the bit start position BitStartPos, and perform big-endian sequence calculation based on the mask array to update the higher octet and the lower octet in turn.

[0048] Furthermore, the big-endian sequence calculation process is as follows:

[0049] Get the current byte start position ByteIndex and the current byte bit start position BitIndex;

[0050] Read the ByteIndex byte value from the buffer and perform a bitwise AND operation with the 8-BitIndex byte value in the high-order byte mask array to obtain the high eight bits; read the ByteIndex+1 byte value from the buffer and perform a bitwise AND operation with the 8-BitIndex byte value in the low-order byte mask array to obtain the low eight bits.

[0051] Shift the high 8 bits left by BitIndex bits and shift the low 8 bits right by 8-BitIndex bits to obtain the result value OutValue, and then output it.

[0052] The beneficial effects of this invention are as follows:

[0053] This invention defines an XML format structure and designs endianness-based serialization and deserialization methods. It enables compile-time field type determination and also calculates field position during compilation, avoiding the computational overhead of runtime type checking. This achieves high-performance serialization and deserialization, improving communication efficiency. Based on the XML format structure, it weakens the connection between implementation logic and specific platforms and languages, freeing coders from the need to focus on the specific serialization and deserialization processes, reducing technical requirements. It also facilitates the portability of endianness-based serialization and deserialization logic modules, enabling plug-and-play functionality and improving coding efficiency. Attached Figure Description

[0054] Figure 1 This is a schematic diagram of the little-endian serialization logic flow.

[0055] Figure 2 This is a schematic diagram of the little-endian deserialization logic flow.

[0056] Figure 3 This is a schematic diagram of the big-endian serialization logic flow.

[0057] Figure 4 This is a schematic diagram of the big-endian deserialization logic flow.

[0058] Figure 5 This is a schematic diagram of the serialization process of directly defining structures in existing technologies.

[0059] Figure 6 This is a schematic diagram of the serialization process for branch judgment in existing technology.

[0060] Figure 7 This is a schematic diagram of the serialization process for polymorphic processing in existing technologies. Detailed Implementation

[0061] The technical solutions in the embodiments of the present invention are clearly and completely described in the following description. Obviously, the described embodiments are only a part of the embodiments of the present invention, and not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those of ordinary skill in the art without creative effort are within the scope of protection of the present invention.

[0062] In the description of the embodiments of the present invention, it should be noted that the indicated orientation or positional relationship is based on the orientation or positional relationship shown in the accompanying drawings, or the orientation or positional relationship in which the product of the invention is conventionally placed during use, or the orientation or positional relationship in which those skilled in the art conventionally understand it during use. This is only for the convenience of describing the present invention and simplifying the description, and is not intended to indicate or imply that the device or element referred to must have a specific orientation, or be constructed and operated in a specific orientation. Therefore, it should not be construed as a limitation of the present invention. Furthermore, the terms "first" and "second" are only used to distinguish descriptions and should not be construed as indicating or implying relative importance.

[0063] In the description of the embodiments of the present invention, it should also be noted that, unless otherwise explicitly specified and limited, the terms "set" and "connection" should be interpreted broadly. For example, they can refer to a fixed connection, a detachable connection, or an integral connection; they can refer to a direct connection or an indirect connection through an intermediate medium. Those skilled in the art can understand the specific meaning of the above terms in the present invention based on the specific circumstances.

[0064] Example 1

[0065] Embodiment 1 of the present invention discloses a binary serialization method, the specific steps of which are as follows:

[0066] S1: Defines an XML format interface based on field type;

[0067] In this embodiment, the definition of the XML format interface is as follows:

[0068] Based on the ICD interface definition, each data field is defined sequentially as its corresponding XML node.

[0069] Specifically, field types can include the types listed in Table 1 below;

[0070] Table 1: Field Type Table

[0071] Serial Number type describe 1 unsigned char 8-bit unsigned integer 2 char 8-bit signed integer 3 unsigned short 16-bit unsigned integer 4 short 16-bit signed integer 5 unsigned int 32-bit unsigned integer 6 int 32-bit signed integer 7 unsigned int64 64-bit unsigned integer 8 int64 64-bit signed integer 9 bool Boolean 10 float Single-precision floating-point number 11 double Double-precision floating-point numbers 12 enum enumerate 13 string String 14 buffer byte buffer 15 array array

[0072] S2: Construct binary serialization and deserialization algorithms based on the XML format interface;

[0073] The binary serialization and deserialization algorithms are constructed based on bitmasks and shift operations using cross-byte bit order processing; they respectively include little-endian binary serialization and deserialization algorithms, as well as big-endian binary serialization and deserialization algorithms.

[0074] Specifically, in this embodiment,

[0075] like Figure 1As shown, the specific execution logic of the little-endian binary serialization algorithm is as follows:

[0076] Input the buffer, input field Value, input bit number BitNum, and set the bit start position BitStartPos;

[0077] Based on the input bit number, calculate the number of bytes ByteNum;

[0078] The specific execution logic for calculating the number of bytes ByteNum is as follows:

[0079] ByteNum = BitNum + 7 >> 3;

[0080] Judge the number of bytes;

[0081] If it is a four-byte data, calculate the values of bits 0-7 as Value1, 8-15 as Value2, 16-23 as Value3, and 24-31 as Value4 respectively;

[0082] Specifically, the execution logic for calculating Value1, Value2, Value3, and Value4 is as follows:

[0083] Value1 = Value & 0xFF;

[0084] Value2 = Value & 0xFF00 >> 8;

[0085] Value3 = Value & 0xFF0000 >> 16;<�

[0086] Value4 = Value & 0xFF000000 >> 24;

[0087] Update the bit start position BitStartPos; and loop to execute the little-endian shift operation calculation steps, and update and process Value1, Value2, Value3, and Value4 in sequence;

[0088] The specific little-endian shift operation calculation steps are as follows:

[0089] Calculate the current byte start position ByteIndex = BitStartPos / 8;

[0090] Calculate the bit start position within the byte ByteIndex = BitStartPos + 7 >> 3;

[0091] Based on the current byte bit values, perform the following shift operations:

[0092] TmpValue = Value << BitIndex, record the 16-bit value;

[0093] The logic for calculating the lower 8 bits is as follows:

[0094] TmpValueL = TmpValue & 0xFF;

[0095] The logic for calculating the high 8 bits is as follows:

[0096] TmpValueH=(TmpValue&0xFF00)>>8;

[0097] Finally, an OR operation is used to assign values ​​to the ByteIndex and ByteIndex+1 bytes of the Buffer, respectively. The execution logic is as follows:

[0098] Buffer[ByteIndex]|=TmpValueL;

[0099] Buffer[ByteIndex+1]|=TmpValueH;

[0100] Based on the assignment calculation results, the values ​​of the corresponding data bits of each byte are obtained sequentially.

[0101] If the data is multi-byte, the value of each byte, ValueIndex, is calculated sequentially starting from the least significant byte, including Value1…ValueN. The bit start position BitStartPos is updated sequentially, and the little-endian shift operation calculation steps described above are executed repeatedly. The specific process will not be repeated here. The value of each byte, ValueIndex, is obtained by sequentially updating the calculation results based on the calculation assignment.

[0102] If it is a single-byte data, then update the bit start position BitStartPos and perform the little-endian shift operation calculation steps described above. The specific process will not be repeated here.

[0103] If the data is two bytes long, the lower octets and higher octets are calculated separately. The specific execution logic is as follows:

[0104] ValueL = Value & 0xFF;

[0105] ValueH = (Value & 0xFF00) >> 8;

[0106] Update the bit start position BitStartPos sequentially, and repeat the above little-endian shift operation calculation steps to update the value of the lower 8 bits ValueL and the value of the higher 8 bits ValueH respectively.

[0107] like Figure 2 As shown in this embodiment, the little-endian binary deserialization algorithm is executed according to the following logic:

[0108] Input buffer, input bit number BitNum, and set bit start position BitStartPos;

[0109] Based on the number of input bits, the number of bytes is calculated at compile time as ByteNum = BitNum + 7 >> 3;

[0110] Define a byte mask array and determine the number of bytes; the specific byte mask array is as follows:

[0111] Low-order byte mask array:

[0112] MaskL={0xFF,0xFE,0xFC,0xF8,0xF0,0xE0,0xC0,0x80,0x00};

[0113] High-order byte mask array:

[0114] MaskL={0x00,0x01,0x03,0x07,0x0F,0x1F,0x3F,0x7F,0xFF};

[0115] If it is four bytes of data, then calculate the value of bits 0-7 (Value1), bits 8-15 (Value2), bits 16-23 (Value3), and bits 24-31 (Value4) respectively.

[0116] Update the bit start position BitStartPos, and perform little-endian sequence calculation based on the byte mask array, and update the value of the corresponding data bit of each byte with the value of the calculation result in turn;

[0117] The specific process is as follows:

[0118] The logic for calculating the starting byte position ByteIndex is as follows:

[0119] ByteIndex = BitStartPos / 8;

[0120] The following logic is used to calculate the starting position BitIndex within a byte:

[0121] BitIndex=BitStartPos+7>>3;

[0122] Read the ByteIndex byte value from the Buffer and perform a bitwise AND operation with the BitIndex bit element in the low-byte mask array to calculate the low-byte value ValueL. The execution logic is as follows:

[0123] ValueL=Buffer[ByteIndex]&MaskL[BitIndex];

[0124] Read the value of the (ByteIndex+1)th byte of the Buffer, and perform a bitwise AND operation with the BitIndex bit element in the high-byte mask array to calculate the high-byte value ValueH. The execution logic is as follows:

[0125] ValueH=Buffer[ByteIndex+1]&MaskH[BitIndex];

[0126] Based on the calculated low byte value ValueL and high byte value ValueH, the following logical calculation is performed:

[0127] TmpValue = (ValueH << 8) | ValueL, representing a 16-bit value;

[0128] The obtained value is shifted to obtain the output result OutValue. The execution logic is as follows:

[0129] OutValue=TmpValue>>BitIndex.

[0130] If it is multi-byte data, calculate the value of each byte ValueIndex sequentially starting from 0, including Value1...ValueN, update the bit start position BitStartPos sequentially, and perform little-endian sequence calculation based on the byte mask array. The specific process of little-endian sequence calculation will not be repeated here. Update the value of each byte.

[0131] The update of each byte's value is as follows:

[0132] The output results are shifted sequentially and then ORed to obtain the final result value. The execution logic is as follows:

[0133] Value = (Value1 < <ByteNum-1)|(Value2<<ByteNum-2)|…(ValueN<<ByteNum-N);

[0134] If it is single-byte data, the little-endian sequence is calculated directly based on the byte mask array, and the result value is recorded and output.

[0135] If the data is two bytes long, the lower octet ValueL and the higher octet ValueH are read, the bit start position BitStartPos is updated, and little-endian sequence calculation is performed based on the byte mask array. The specific process of little-endian sequence calculation will not be repeated here. The lower octet and the higher octet are updated sequentially, and a shift operation and OR operation are performed based on the lower octet and the higher octet to obtain the final result. The execution logic is as follows:

[0136] Value = (ValueH << 8) | ValueL.

[0137] like Figure 3 As shown in this embodiment, the big-endian binary serialization algorithm is executed according to the following logic:

[0138] Input buffer, input field value, input bit number bit number bit number bit number bit start position bitstartPos;

[0139] Calculate the number of bytes (ByteNum) based on the input bit length.

[0140] The specific execution logic for calculating the number of bytes (ByteNum) is as follows:

[0141] ByteNum = BitNum + 7 >> 3;

[0142] Define a byte mask array and determine the number of bytes; the specific byte mask array is as follows:

[0143] Low-order byte mask array:

[0144] MaskL={0xFF,0xFE,0xFC,0xF8,0xF0,0xE0,0xC0,0x80,0x00};

[0145] High-order byte mask array:

[0146] MaskL={0x00,0x01,0x03,0x07,0x0F,0x1F,0x3F,0x7F,0xFF};

[0147] Determine the number of bytes;

[0148] If it is four bytes of data, then calculate the value of bits 24-31 (Value1), bits 16-23 (Value2), bits 8-15 (Value3), and bits 0-7 (Value4) respectively.

[0149] Specifically, the calculation logic for Value1, Value2, Value3, and Value4 is as follows:

[0150] Value1=Value&0xFF000000>>24;

[0151] Value2=Value&0xFF0000>>16;

[0152] Value3 = Value & 0xFF00 >> 8;

[0153] Value4 = Value & 0xFF;

[0154] Update the bit start position BitStartPos; and repeatedly execute the big-endian shift operation calculation steps to update and process Value1, Value2, Value3, and Value4 in sequence;

[0155] The calculation steps for the big-endian shift operation are as follows:

[0156] Calculate the current byte start position ByteIndex = BitStartPos / 8;

[0157] Calculate the starting position of the byte's internal bit: ByteIndex = BitStartPos + 7 >> 3;

[0158] The shift and AND operation is performed based on the low-order byte mask array to calculate the value of the lower 8 bits. The execution logic is as follows:

[0159] ValueL=(Value&MaskL[BitIndex])>>BitIndex;

[0160] Based on the high-order byte mask array, a shift and AND operation is performed to calculate the high 8-bit value. The execution logic is as follows:

[0161] ValueH=(Value&MaskH[BitIndex])>>(8-BitIndex);

[0162] Finally, an OR operation is used to assign values ​​to the ByteIndex and ByteIndex+1 bytes of the Buffer, respectively. The execution logic is as follows:

[0163] Buffer[ByteIndex]|=TmpValueL;

[0164] Buffer[ByteIndex+1]|=TmpValueH;

[0165] Based on the assignment calculation results, the values ​​of the corresponding data bits of each byte are obtained sequentially.

[0166] If the data is multi-byte, the value of each byte, ValueIndex, is calculated sequentially starting from the high byte, including Value1...ValueN. The bit start position BitStartPos is updated sequentially, and the big-endian shift operation calculation steps described above are executed repeatedly. The specific process will not be repeated here. The value of each byte, ValueIndex, is obtained by sequentially updating the calculation results based on the calculation assignment.

[0167] If it is a single-byte data, then update the bit start position BitStartPos and perform the above big-endian shift operation calculation steps. The specific process will not be repeated here.

[0168] If the data is two bytes long, the high octet and low octet are calculated separately. The specific execution logic is as follows:

[0169] ValueH = (Value & 0xFF00) >> 8;

[0170] ValueL = Value & 0xFF;

[0171] Update the bit start position BitStartPos sequentially, and repeat the above big-endian shift operation calculation steps to update the value of the high eight bits ValueH and the value of the low eight bits ValueL respectively.

[0172] like Figure 4 As shown in this embodiment, the big-endian binary deserialization algorithm is executed according to the following logic:

[0173] Input buffer, input bit number BitNum, and set bit start position BitStartPos;

[0174] Based on the number of input bits, the number of bytes is calculated at compile time as ByteNum = BitNum + 7 >> 3;

[0175] Define a byte mask array and determine the number of bytes; the specific byte mask array is as follows:

[0176] Low-order byte mask array:

[0177] MaskL={0xFF,0xFE,0xFC,0xF8,0xF0,0xE0,0xC0,0x80,0x00};

[0178] High-order byte mask array:

[0179] MaskL={0x00,0x01,0x03,0x07,0x0F,0x1F,0x3F,0x7F,0xFF};

[0180] If it is four bytes of data, then calculate the value of bits 24-31 (Value1), bits 16-23 (Value3), bits 8-15 (Value2), and bits 0-7 (Value1) respectively.

[0181] Update the bit start position BitStartPos, and perform big-endian sequence calculation based on the byte mask array, and update the value of the corresponding data bit of each byte with the value of the calculation result in turn;

[0182] The specific process is as follows:

[0183] The logic for calculating the starting byte position ByteIndex is as follows:

[0184] ByteIndex = BitStartPos / 8;

[0185] The following logic is used to calculate the starting position BitIndex within a byte:

[0186] BitIndex=BitStartPos+7>>3;

[0187] Read the ByteIndex value of the Buffer and perform a bitwise AND operation with the 8-BitIndex element in the high-byte mask array to calculate the high-byte value ValueH. The execution logic is as follows:

[0188] ValueH=Buffer[ByteIndex]&MaskH[8-BitIndex];

[0189] Read the value of the (ByteIndex+1)th byte of the Buffer and perform a bitwise AND operation with the 8-BitIndex element in the low-byte mask array to calculate the low-byte value ValueL. The execution logic is as follows:

[0190] ValueH=Buffer[ByteIndex+1]&MaskL[8-BitIndex];

[0191] Based on the calculated high byte value ValueH and low byte value ValueL, a shift OR operation is performed to obtain the output result. The execution logic is as follows:

[0192] OutValue = (ValueH <<BitIndex)|(ValueL> >8-BitIndex);

[0193] If it is multi-byte data, calculate the value of each byte ValueIndex sequentially starting from 0, including Value1...ValueN, update the bit start position BitStartPos sequentially, and perform big-endian sequence calculation based on the byte mask array. The specific process of the big-endian sequence calculation will not be repeated here. Update the value of each byte.

[0194] The update of each byte's value is as follows:

[0195] The output results are shifted sequentially and then ORed to obtain the final result value. The execution logic is as follows:

[0196] Value = (Value1 < <ByteNum-1)|(Value2<<ByteNum-2)|…(ValueN<<ByteNum-N);

[0197] If it is single-byte data, the big-endian sequence is calculated directly based on the byte mask array, and the result value is recorded and output.

[0198] If the data is two bytes long, the lower octet ValueL and the higher octet ValueH are read, the bit start position BitStartPos is updated, and big-endian sequence calculation is performed based on the byte mask array. The specific process of big-endian sequence calculation will not be repeated here. The higher octet and the lower octet are updated sequentially, and a shift operation and OR operation are performed based on the higher octet and the lower octet to obtain the final result. The execution logic is as follows:

[0199] Value = (ValueH << 8) | ValueL.

[0200] S3: Retrieves data fields from the cache, reads data fields, and implements data transmission based on the XML format interface and the constructed binary serialization and deserialization algorithms.

[0201] This invention is not limited to the specific embodiments described above. The invention extends to any new feature or combination disclosed in this specification, as well as any new method or process step or combination disclosed herein.

Claims

1. A binary serialization method, characterized in that, include: S1: Defines an XML format interface based on field type; S2: Construct binary serialization and deserialization algorithms based on the XML format interface; The binary serialization and deserialization algorithms include little-endian binary serialization and deserialization algorithms, as well as big-endian binary serialization and deserialization algorithms. The little-endian binary serialization algorithm executes as follows: Read the field value from the cache, get the input number of bits BitNum, and set the bit start position BitStartPos; Calculate the number of bytes (ByteNum) based on the input bit length. If it is four bytes of data, calculate the value of the corresponding data bit of each byte from the least significant bit to the most significant bit, update the bit start position BitStartPos, and perform a shift operation to obtain the value of the corresponding data bit of each byte in sequence; If the data is multi-byte, calculate the value of each byte sequentially starting from the least significant byte, perform shift operations, update the bit start position BitStartPos, and process to obtain the value of each byte; If it is a single byte of data, update the bit start position BitStartPos and perform a shift operation; If it is two-byte data, calculate the lower octet and the higher octet respectively, update the bit start position BitStartPos, and perform a shift operation to obtain the lower octet and the higher octet values ​​in sequence. The little-endian binary deserialization algorithm executes as follows: Input buffer, get the number of input bits BitNum, and set the bit start position BitStartPos; The number of bytes, ByteNum, is calculated at compile time based on the number of bits input. Set a byte mask array and determine the number of bytes; If it is four-byte data, calculate the value of the corresponding data bit of each byte, update the bit start position BitStartPos, calculate the byte start position ByteIndex and the bit start position within the byte BitIndex, read the value of the data field in the cache, and perform little-endian sequence calculation based on the byte mask array, and update the value of the corresponding data bit of each byte with the value of the calculation result in turn; If the data is multi-byte, calculate the value of each byte sequentially starting from the least significant byte, update the bit start position BitStartPos sequentially, and perform little-endian sequence calculation based on the byte mask array to update the value of each byte. If it is single-byte data, the little-endian sequence is calculated directly based on the byte mask array, and the result value is recorded and output. If it is double-byte data, read the lower octet and the higher octet, update the bit start position BitStartPos, and perform little-endian sequence calculation based on the byte mask array to update the lower octet and the higher octet in turn. The big-endian binary serialization algorithm executes as follows: Read the field value from the cache, get the input number of bits BitNum, input it into the cache Buffer, and set the bit start position BitStartPos; Calculate the number of bytes (ByteNum) based on the input bit width, set the byte mask array, and determine the number of bytes; If it is four bytes of data, calculate the value of the corresponding data bit of each byte from the high bit to the low bit, update the bit start position BitStartPos, and perform shift operation on the value of the corresponding data bit of each byte in turn to obtain the value of the corresponding data bit of each byte in turn. If the data is multi-byte, calculate the value of each byte sequentially starting from the most significant byte, perform shift operations, update the bit start position BitStartPos, and perform shift operations sequentially based on the byte mask array to update the value of all bytes. If it is a single byte of data, update the bit start position BitStartPos, and perform a shift operation based on the byte mask array to update the byte value; If it is two-byte data, calculate the high octet and the low octet value respectively, update the bit start position BitStartPos, and perform a shift operation based on the byte mask array to obtain the value of the high octet and the low octet value in turn. The big-endian binary deserialization algorithm executes as follows: Input buffer, get the number of input bits BitNum, and set the bit start position BitStartPos; The number of bytes, ByteNum, is calculated at compile time based on the number of bits input. Set a byte mask array and determine the number of bytes; If it is four-byte data, calculate the value of the corresponding data bit of each byte from the high bit to the low bit, update the bit start position BitStartPos, calculate the byte start position and the byte inner bit start position, read the value of the data field in the cache, and perform big endian sequence calculation based on the byte mask array, and update the value of the corresponding data bit of each byte with the value of the calculation result in turn; If the data is multi-byte, calculate the value of each byte sequentially starting from the least significant byte, update the bit start position BitStartPos sequentially, and perform big-endian sequence calculation based on the mask array to update the value of each byte. If it is single-byte data, perform big-endian sequence calculation directly based on the mask array, and record the result value for output; If it is double-byte data, read the lower octet and the higher octet, update the bit start position BitStartPos, and perform big-endian sequence calculation based on the mask array to update the higher octet and the lower octet in turn. S3: Retrieves data fields from the cache, reads data fields, and implements data transmission based on the XML format interface and the constructed binary serialization and deserialization algorithms.

2. The binary serialization method according to claim 1, characterized in that, The defined XML format interface is as follows: Based on the ICD interface definition, each data field is defined sequentially as its corresponding XML node.

3. The binary serialization method according to claim 2, characterized in that, The binary serialization and deserialization algorithms are constructed using cross-byte bit order processing based on bit masks and shift operations.

4. The binary serialization method according to claim 1, characterized in that, The little-endian sequence calculation process is as follows: Get the current byte start position ByteIndex and the current byte bit start position BitIndex; Perform a bitwise AND operation between the current byte value and the BitIndex value in the low-order byte mask array, and then perform a right shift operation to obtain the low octet value; perform a bitwise AND operation between the current byte value and the BitIndex value in the high-order byte mask array, and then perform a left shift operation to obtain the high octet value; Assign the lower octet to the cached ByteIndex byte, and assign the higher octet to the cached ByteIndex+1 byte.

5. The binary serialization method according to claim 1, characterized in that, The big-endian sequence calculation process is as follows: Get the current byte start position ByteIndex and the current byte bit start position BitIndex; Read the ByteIndex byte value from the buffer and perform a bitwise AND operation with the 8-BitIndex byte value in the high-order byte mask array to obtain the high eight bits; read the ByteIndex+1 byte value from the buffer and perform a bitwise AND operation with the 8-BitIndex byte value in the low-order byte mask array to obtain the low eight bits. Shift the high 8 bits left by BitIndex bits and shift the low 8 bits right by 8-BitIndex bits to obtain the result value OutValue, and then output it.