Message coding method and message decoding method
By calculating the length of file content and text content, accurately determining the total message length, the problem of POJO object encoding decoder is solved in the complex logic and low efficiency of processing different protocol message formats, and the efficiency of message processing and decoupling of information types is achieved.
Patent Information
- Application Number
- CN202411677059.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-11-21
- Publication Date
- 2025-02-25
AI Technical Summary
When POJO object encoding and decoder is used to process message formats of different protocols, the logic is complex and inefficient, resulting in the complexity of message encoding and decoding processes.
By calculating the length of the file content and text content and the total length of the message, accurately determine the overall length of the message, simplifying the verification process and protocol attribute processing, clearly distinguishing the text content and file content parts, and realizing the decoupling of information types.
It improves the efficiency of message processing, avoids interlacing of text content and file content, and simplifies the checksum protocol attribute processing process.
Smart Images

Figure CN119520458A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of data processing technology, and in particular to a message encoding method and a message decoding method. Background Art
[0002] When developing network communication programs, use POJO (Plain Old Java Object) object codecs to process custom serialized message objects. The function of this codec is to convert the received message content into Java objects and support the development of custom communication protocols.
[0003] During use, the POJO object encoder and decoder needs to define a Java object specifically used to receive messages. This object contains multiple protocols, and the information formats defined by different protocols (message formats include message headers, message bodies, check bits, etc.) may be different. Since the information formats defined by each protocol may be different, when the POJO object encoder and decoder processes the encoding and decoding logic, different logic needs to be implemented in the POJO object encoder and decoder to process various message formats for the message formats of different protocols, which makes the encoding and decoding of received messages complicated. At the same time, due to the complexity of the encoding and decoding logic of the message, as well as the interweaving and entanglement between different information formats, the POJO object encoder and decoder is less efficient in processing messages. Summary of the invention
[0004] Based on the above problems, the present application provides a message encoding method and a message decoding method, which can improve the efficiency of POJO object encoder and decoder in the process of processing messages.
[0005] The embodiments of the present application disclose the following technical solutions:
[0006] A method for encoding a message, the method comprising:
[0007] Get the file content and text content to be encoded;
[0008] Calculating the file content length of the file content, and calculating the preliminary text content length of the text content;
[0009] Calculate the total length of the message according to the file content length and the preliminary text content length, and convert the preliminary text content length into a text content length;
[0010] The total length of the message, the length of the text content, the text content and the file content are sequentially concatenated to obtain an encoded message.
[0011] In a possible implementation, calculating the file content length of the file content includes:
[0012] Encoding the file content into a fixed encoding form;
[0013] The length of the file content in the fixed encoding form is calculated as the file content length.
[0014] In a possible implementation, calculating the preliminary text content length of the text content includes:
[0015] Encoding the text content into a fixed encoding form;
[0016] The length of the text content in the fixed encoding form is calculated as the preliminary text content length.
[0017] In a possible implementation, the calculating the total message length according to the file content length and the preliminary text content length includes:
[0018] Sum the length of the file content and the length of the preliminary text content;
[0019] Add the sum of the file content length and the preliminary text content length to the fixed length to obtain a preliminary total length;
[0020] The preliminary total length is encoded into a fixed encoding form to obtain the total length of the message; the total length of the message conforms to the first fixed-length data type.
[0021] In a possible implementation, converting the preliminary text content length into a data type to obtain the text content length includes:
[0022] The text content length is encoded into a fixed encoding form to obtain the text content length; the text content length conforms to the second fixed-length data type.
[0023] A method for decoding a message, the method comprising:
[0024] Obtaining the first code in the message to be decoded that is encoded to a first preset number of bits, and obtaining a first segment of code; the message to be decoded consists of a total message length, a text content length, a text content, and a file content; the total message length, the text content length, the text content, and the file content are all codes in a fixed coding form;
[0025] Calculating the length of the first segment code to obtain the total length of the message, and receiving the message to be decoded according to the total length of the message;
[0026] Obtaining the code from the first preset number of bits to the second preset number of bits in the received message to be decoded, to obtain a second segment code;
[0027] Calculate the length of the second segment code to obtain the length of the text content;
[0028] The text content is extracted from the received message to be decoded according to the length of the text content, and the text content is decoded in a fixed encoding form to obtain text content information.
[0029] In a possible implementation, calculating the length of the first segment code to obtain the total message length includes:
[0030] Convert the first segment of code into a first character string;
[0031] The first character string is converted into a number to obtain the total length of the message.
[0032] In a possible implementation, calculating the length of the second segment code to obtain the length of the text content includes:
[0033] Convert the second segment of code into a second character string;
[0034] The second character string is converted into a number to obtain the length of the text content.
[0035] In a possible implementation, the method further includes:
[0036] When the message to be decoded is received according to the total length of the message and the total length of the message is greater than the preset length, the length of the received message to be decoded is calculated to obtain the received length, and the message to be decoded in the buffer area is placed in the memory of the executable program;
[0037] Calculate the remaining length according to the received length and the total length of the message;
[0038] The message to be decoded continues to be received according to the remaining length until the remaining length returns to zero.
[0039] In a possible implementation, extracting the text content from the received message to be decoded according to the length of the text content, and decoding the text content in a fixed encoding form to obtain text content information includes:
[0040] When the total length of the message is greater than the length of the text content, the text content is extracted from the received message to be decoded according to the length of the text content, and the text content is decoded in a fixed coding form to obtain text content information.
[0041] Compared with the prior art, this application has the following beneficial effects:
[0042] The present application provides a method for encoding a message and a method for decoding a message. Specifically, when executing the method for encoding a message provided by an embodiment of the present application, the file content and text content to be encoded can be obtained first. Then, the file content length of the file content and the preliminary text content length of the text content are calculated, and the total length of the message is calculated according to the file content length and the preliminary text content length, and the preliminary text content length is converted into a data type to obtain the text content length. Then the total length of the message, the text content length, the text content and the file content are sequentially spliced to obtain the encoded message. The present application can accurately determine the overall length of the message by calculating the length of the file content and the text content and the total length of the message, so that the receiving end of the message can directly extract the complete message content according to the length information, without the need for additional verification and processing, thereby simplifying the verification process and the additional protocol attribute processing process. At the same time, the text content and the file content part in the message are clearly distinguished, and the decoupling of different information types is realized to avoid the situation where the text content and the file content are intertwined and entangled in the process of processing the message, and the efficiency of message processing is improved.
[0043] "Decoupling of different information types" refers to separating different types of information during data processing and transmission. BRIEF DESCRIPTION OF THE DRAWINGS
[0044] In order to more clearly illustrate the technical solutions in this embodiment or the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without creative work.
[0045] Figure 1a A method flow chart of a message encoding method provided in an embodiment of the present application;
[0046] Figure 1b A method flow chart of a method for calculating the total length of a message provided in an embodiment of the present application;
[0047] Figure 1c A message structure of a coded message provided in an embodiment of the present application;
[0048] Figure 2a A method flow chart of a message decoding method provided in an embodiment of the present application;
[0049] Figure 2b A method flow chart of a method for receiving a message to be decoded provided in an embodiment of the present application;
[0050] Figure 3 A schematic diagram of the structure of a message encoding device provided in an embodiment of the present application;
[0051] Figure 4 A schematic diagram of the structure of a message decoding device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0052] To facilitate understanding of the technical solutions provided by the embodiments of the present application, the background technology involved in the embodiments of the present application will be described below.
[0053] When developing network communication programs, use POJO object codecs to handle custom serialized message objects. The function of POJO object codecs is to convert the received message content into Java objects and support the development of custom communication protocols.
[0054] When using POJO object encoders and decoders, you need to define a Java object for receiving messages. This object contains multiple protocols, and different protocols may define different information formats, including message headers, message bodies, check bits, etc. Therefore, when processing encoding and decoding logic, different logic needs to be implemented in POJO object encoders and decoders for message formats of different protocols to handle various message formats, which makes encoding and decoding complicated.
[0055] Due to the complexity of the encoding and decoding logic of the message and the interweaving of different information formats, the POJO object encoder and decoder are less efficient in processing messages.
[0056] In order to solve this problem, a method for encoding a message and a method for decoding a message are provided in an embodiment of the present application. The file content and text content to be encoded are first obtained, and the file content length of the file content and the preliminary text content length of the text content are calculated. Then the total message length is calculated according to the file content length and the preliminary text content length, and the preliminary text content length is converted into a data type to obtain the text content length. Then, the total message length, the text content length, the text content and the file content are sequentially spliced to obtain the encoded message. The present application focuses more on the length information of the message itself, reducing the process of checking the message itself and processing additional protocol attributes. At the same time, the present application clearly distinguishes between the text content part and the file content part, and realizes the decoupling of different information types. When processing messages, the text content and the file content are avoided from being intertwined, thereby improving the efficiency of message processing.
[0057] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of this application.
[0058] See also Figure 1a , which is a flow chart of a message encoding method provided by an embodiment of the present application, such as Figure 1a As shown, the message encoding method may include steps S101-S104:
[0059] S101: Obtain the file content and text content to be encoded.
[0060] Among them, file content refers to the binary data of a file or the encoded file content, and text content refers to the text information contained in the message. The text content part only contains the text information of this message, and the file content part only contains the file itself, without involving other additional information. For example, {"messageType":"1","data":"text content"} is text content.
[0061] In the message encoding process, the file content and text content to be encoded need to be converted into a specific encoding form to facilitate processing and parsing during network transmission or storage. File content and text content are usually the main information carriers of messages and need to be extracted and encoded into a message in a specific format for transmission to the recipient.
[0062] If the file contains the content to be encoded, the binary data or text data of the file can be obtained through the file read operation. The corresponding file operation function or library can be used to read the file content and store it in memory for subsequent processing.
[0063] The text content is usually the text information part of the message, which can be obtained through user input, text file reading, network request, etc. For the text content input by the user, it can be obtained through the interface input box or command line parameters. For the text file content, the file reading operation can be used to obtain the text content. For the text content obtained by network request, the text data on the network can be obtained by sending HTTP (Hypertext Transfer Protocol) request, etc.
[0064] In a possible implementation, the file content and text content to be encoded are stored in variables. When the file content and text content to be encoded are stored in variables, the types of these variables can be checked through functions provided by programming languages or tools (such as the type() function in Python). If the type of the variable is a string, the data stored in the variable is regarded as text content; conversely, if the type of the variable is other types, such as byte stream, file object, etc., it means that the variable stores file content.
[0065] In one possible implementation, obtaining the content of the file to be encoded can be achieved through the following steps: (1) Opening the file: First, you need to open the file containing the content of the file to be encoded. This can be achieved through the functions or libraries provided by the file system. (2) Reading the file content: Next, read the file content from the open file. The specific reading method depends on the file type and programming language. The entire file content can be read at once or in blocks. (3) Processing the file content: Process the file content as needed, such as encoding, encryption, etc. (4) Prepare encoded data: Convert the processed file content into a form suitable for encoding, such as binary data or a specific encoding format.
[0066] The purpose of obtaining the file content and text content to be encoded is to process and transmit this information. The specific acquisition method depends on the source and storage method of the message content. In practical applications, it is necessary to select a suitable method to obtain the file content and text content to be encoded according to the specific situation.
[0067] S102: Calculate the file content length of the file content, and calculate the preliminary text content length of the text content.
[0068] The purpose of calculating the file content length of the file content and calculating the preliminary text content length of the text content is to accurately calculate the total length of the message during the message encoding process and perform subsequent data processing.
[0069] Specifically, for file content, the length of the file content can be calculated by reading the number of bytes or characters of the file. This length is usually in bytes, and the file size information can be directly obtained to determine the length of the file content. For text content, the length of the text content can be calculated by counting the number of characters in the text. This length is usually in characters, and the length of the text content can be obtained by directly counting the number of characters in the text.
[0070] In a possible implementation, calculating the file content length of the file content includes:
[0071] The file content is encoded into a fixed encoding form, and the length of the file content in the fixed encoding form is calculated as the file content length.
[0072] Converting file contents into a fixed encoding format can make it easier for computers to process and store the file contents.
[0073] Specifically, the steps of encoding the file content into a fixed encoding format and calculating its length are as follows:
[0074] (1) Select a fixed encoding format: First, you need to determine which fixed encoding format to use to encode the file content, such as UTF-8 (Unicode Transformation Format-8-bit), UTF-16 (Unicode Transformation Format-16-bit), etc.
[0075] (2) Encoding file content: Use the selected fixed encoding format to convert the file content into an encoding format. This usually involves converting the characters in the file content into a byte sequence in the corresponding encoding format.
[0076] (3) Calculate the encoded length: Calculate the length of the encoded file content. Since characters in different encoding forms occupy different numbers of bytes, the encoded length may be different from the original file content length.
[0077] Through the above steps, the file content can be encoded into a fixed encoding format, and its length in the encoding format can be calculated as the file content length.
[0078] In a possible implementation, calculating the preliminary text content length of the text content includes:
[0079] The text content is encoded into a fixed encoding form, and the length of the text content in the fixed encoding form is calculated as the preliminary text content length.
[0080] Likewise, converting text content into a fixed encoding format can make it easier for computers to process and store the text content.
[0081] Specifically, the steps of encoding the text content into a fixed encoding form and calculating its length are as follows:
[0082] (1) Select a fixed encoding format: First, you need to determine which fixed encoding format to encode the text content into, such as UTF-8, UTF-16, etc.
[0083] (2) Encoding text content: Using the selected fixed encoding form, the text content is encoded and converted. This usually involves converting the characters in the text content into a byte sequence in the corresponding encoding form.
[0084] (3) Calculate the encoded length: Calculate the length of the encoded text content. Since characters in different encoding forms occupy different numbers of bytes, the encoded length may be different from the original file content length.
[0085] Through the above steps, the text content can be encoded into a fixed encoding form, and its length in the encoding form can be calculated as the text content length.
[0086] Optionally, the fixed encoding format may be, but is not limited to, a binary format.
[0087] For example, the file content is converted into binary code, that is, [0x8D,…,0xA4], and then the length of the file content in binary code, that is, the length of the preliminary text content, is calculated to be 9788.
[0088] Convert the text content {"messageType":"1","data":"text content"} into binary code, i.e. [0x7B,…,0x7D]. Then calculate the length of the text content in binary code, i.e. the length of the file content, which is 41.
[0089] S103: Calculate the total length of the message according to the file content length and the preliminary text content length, and convert the preliminary text content length into a text content length.
[0090] The total length of a message refers to the total length of a message during transmission or storage. In network communication, the total length of a message is very important for determining the boundaries of the message so that the receiving end can correctly parse and process the message. In data processing and programming, calculating the total length of a message also helps to ensure the integrity and accuracy of the data, especially when processing binary data or when the data size needs to be precisely controlled.
[0091] See also Figure 1b , Figure 1b A method flow chart of a method for calculating the total length of a message provided in an embodiment of the present application, correspondingly, in S103, the total length of the message is calculated according to the file content length and the preliminary text content length, including A1-A3:
[0092] A1: Sum the length of the file content and the length of the preliminary text content.
[0093] This step is to obtain the original data length of the total message length. It is necessary to add the file content length and the preliminary text content length to get their sum.
[0094] A2: Add the sum of the file content length and the preliminary text content length to the fixed length to obtain a preliminary total length.
[0095] The sum of the file content length obtained in the first step and the preliminary text content length is added to a fixed length to obtain the preliminary total message length.
[0096] It should be noted that the fixed length can be but is not limited to being set to 48. The fixed length can be any predetermined value used as a fixed part of the total message length. This application does not impose specific restrictions on the size of the fixed length.
[0097] A3: Encode the preliminary total length into a fixed encoding form to obtain the total length of the message; the total length of the message conforms to the first fixed-length data type.
[0098] The preliminary total length is encoded in the specified encoding format to obtain the final total length of the message. The encoded result is usually a byte sequence that represents the encoding format of the total length of the message. The total length of the message conforms to the first fixed-length data type.
[0099] Optionally, the fixed encoding format may be, but is not limited to, a binary format.
[0100] It should be noted that the total length of the message conforming to the first fixed-length data type means that the total length of the message is a 32-bit string. In the process of encoding the preliminary total length into a fixed-code form to obtain the total length of the message, if the total length of the message obtained is less than 32 bits, 0 is added to the front of the total length of the message until the total length of the message is a 32-bit string.
[0101] In a possible implementation, converting the preliminary text content length into a data type to obtain the text content length includes:
[0102] The text content length is encoded into a fixed encoding form to obtain the text content length; the text content length conforms to the second fixed-length data type.
[0103] Optionally, the fixed encoding format may be, but is not limited to, a binary format.
[0104] It should be noted that the text content length conforming to the second fixed-length data type means that the text content length is a 16-bit string. In the process of encoding the text content length into a fixed encoding form to obtain the text content length, if the obtained text content length is less than 16 bits, 0 is added to the front of the text content length until the text content length is a 16-bit string.
[0105] S104: Concatenate the total message length, the text content length, the text content and the file content in sequence to obtain an encoded message.
[0106] In order to accurately restore the structure and content of the message during data transmission or storage, it is necessary to concatenate the obtained data in the order of total message length, text content length, text content and file content to obtain the encoded message.
[0107] See also Figure 1c , Figure 1c A message structure of an encoded message provided in an embodiment of the present application.
[0108] The specific steps of concatenating the total message length, text content length, text content, and file content in a certain order to form a complete encoded message are as follows:
[0109] The converted contents are concatenated in a predetermined order to form a complete coded message. The usual order is to write the total length of the message first, then the length of the text content, then the text content itself, and finally the file content. Then, the concatenated coded message is further encoded, and Base64 encoding, Hex encoding, etc. can be used to convert the message into a format that can be transmitted or stored.
[0110] Through the above steps, the total message length, text content length, text content and file content can be concatenated together in a specific order and finally encoded into a form that can be transmitted or stored.
[0111] Based on the contents of S101-S104, it can be known that the file content and text content to be encoded are first obtained, and the file content length of the file content and the preliminary text content length of the text content are calculated. Next, the total length of the message is calculated based on the file content length and the preliminary text content length, and the preliminary text content length is converted into a data type to obtain the text content length. Finally, the total message length, the text content length, the text content and the file content are sequentially spliced to obtain the encoded message. The present application can accurately determine the overall length of the message by calculating the length of the file content and the text content and the total message length, simplifies the verification process and the additional protocol attribute processing process, and makes the message processing more efficient. At the same time, the text content and the file content part in the message are clearly distinguished, and the decoupling of different information types is realized to avoid the situation where the text content and the file content are intertwined and entangled in the process of processing the message, which is conducive to improving the efficiency of message processing.
[0112] Based on the above-mentioned message encoding method embodiment, the present application embodiment also provides a message decoding method. Figure 2a , Figure 2a A method flow chart of a message decoding method provided in an embodiment of the present application. Figure 2a As shown, the method includes S201-S205:
[0113] S201: Obtain the first code in the message to be decoded that is encoded up to a first preset number of bits to obtain a first segment of code.
[0114] The message to be decoded consists of the total message length, text content length, text content and file content. The total message length, text content length, text content and file content are all encoded in a fixed encoding form.
[0115] In order to avoid the problem of packet sticking and unpacking of the message to be decoded during data communication, it is necessary to obtain the first code in the message to be decoded that is encoded up to the first preset number of bits to obtain the first segment of code.
[0116] Optionally, the fixed encoding format may be, but is not limited to, a binary format.
[0117] Optionally, the first preset number of bits may be but is not limited to 32 bits.
[0118] The process of obtaining the first code up to the first preset number of bits in the message to be decoded and obtaining the first code segment is as follows:
[0119] Determine the range of the first code: Determine the range of the number of digits occupied by the first code based on the preset number of digits.
[0120] Extract the first code from the message to be decoded: According to the determined code bit range, intercept this part of the code data from the message to be decoded to obtain the first code. This code includes the total length of the message, the length of the text content, the text content and the encoded data of the file content, all of which are expressed in a fixed code form.
[0121] S202: Calculate the length of the first code segment to obtain the total length of the message, and receive the message to be decoded according to the total length of the message.
[0122] Before sending a message, the sender will calculate the length of the message and encode the length into a specific format as the message header. The length information of this encoding is the length of the first encoding. When receiving the message, the receiver can obtain the total length of the message by parsing the length information of this encoding.
[0123] The receiver receives the actual message content, that is, the message to be decoded, according to the total length of the received message. After knowing the total length of the message, the receiver can accurately receive the message content of the corresponding length to ensure the integrity of the data.
[0124] By including information about the total length of the message in the message, the receiver can be helped to accurately receive and parse the message content, thereby achieving reliable data communication.
[0125] In a possible implementation manner, calculating the length of the first segment code to obtain the total message length includes:
[0126] The first code is converted into a first character string, and the first character string is converted into a number to obtain the total length of the message.
[0127] Convert the first code segment into a string according to certain rules. During the conversion process, it is necessary to determine how to map the binary data to the string representation, which can be a simple binary to character mapping (for example, converting every 8 bits of binary to one character) or other custom conversion rules. After obtaining the first string, parse the string into a number according to the corresponding rules. This process is called string to number conversion, which can be implemented using functions or methods provided by the programming language.
[0128] By converting the first code into a string and then converting the string into a number, the total length of the message can be obtained, so that the received message can be accurately parsed and subsequently processed.
[0129] S203: Obtain the code from the first preset number of bits to the second preset number of bits in the received message to be decoded, and obtain a second segment code.
[0130] Count from the first preset number of bits in the message to be decoded to the second preset number of bits, and obtain the code of the segment as the second segment code.
[0131] For example, if the first preset number of bits is 32 and the second preset number of bits is 16, then the range of bits to be intercepted is the 16-bit code starting from 32.
[0132] Optionally, the second preset bit number may be but is not limited to 16 bits.
[0133] See also Figure 2b , Figure 2b A method flow chart of a method for receiving a message to be decoded provided in an embodiment of the present application, the specific implementation of the method for receiving a message to be decoded may include B1-B3:
[0134] B1: When the message to be decoded is received according to the total length of the message and the total length of the message is greater than the preset length, the length of the received message to be decoded is calculated to obtain the received length, and the message to be decoded in the buffer area is placed in the memory of the executable program.
[0135] When the total length of the message to be decoded received by the receiver is greater than the preset length, the receiver needs to take the following steps:
[0136] Calculate the length of the received message to be decoded and obtain the received length: The receiver needs to determine the actual length of the received message, that is, the amount of data contained in the message. This length information is usually transmitted as part of the message, and the receiver needs to parse this length information to calculate the length of the received message.
[0137] Put the messages to be decoded in the buffer into the memory of the executable program: The received message will be temporarily stored in the buffer. The receiver needs to take the message out of the buffer and put it into the memory of the executable program for subsequent processing. Putting the messages to be decoded in the buffer into the memory of the executable program can free up space to receive the remaining messages to be decoded.
[0138] Optionally, the preset length may be but is not limited to 65535.
[0139] B2: Calculate the remaining length according to the received length and the total length of the message.
[0140] When processing received messages, the length of the message that needs to be received, that is, the remaining length, can be calculated by the difference between the length of the received message and the total length of the message.
[0141] Specifically, if a portion of the message has been received and processed, and the length of this portion of the message (i.e., the received length) is known, and the total length of the entire message is also known, then by calculating the difference between the two, the remaining length, i.e., the length of the message that still needs to be received, can be obtained.
[0142] By calculating the remaining length, the remaining message data can be continuously received until the message is completely received, so that the message can be completely transmitted and processed.
[0143] B3: Continue receiving the message to be decoded according to the remaining length until the remaining length returns to zero.
[0144] S204: Calculate the length of the second code segment to obtain the text content length.
[0145] The length of the second encoded segment is calculated to obtain the length of the text content. By calculating the length of the second encoded segment, the length of the original text content can be inferred.
[0146] In a possible implementation, calculating the length of the second segment code to obtain the length of the text content includes:
[0147] The second segment code is converted into a second character string, and the second character string is converted into a number to obtain the length of the text content.
[0148] Convert the second segment of code into a string according to certain rules. During the conversion process, it is necessary to determine how to map the binary data to the string representation, which can be a simple binary to character mapping (for example, converting every 8 bits of binary to one character) or other custom conversion rules. After obtaining the second string, parse the string into a number according to the corresponding rules. This process is called string to number conversion, which can be implemented using functions or methods provided by the programming language.
[0149] S205: extracting the text content from the received message to be decoded according to the length of the text content, and decoding the text content in a fixed encoding form to obtain text content information.
[0150] After the length of the text content in the message to be decoded is determined, data of the corresponding length can be intercepted from the received message to be decoded as the text content. These data may be text encoded in a specific format. The extracted text content is decoded according to a predetermined encoding form. For example, if the text content is encoded in UTF-8 encoding, then the corresponding decoding algorithm can be used to decode it into readable text. After decoding, the obtained text content is the text content information.
[0151] It should be noted that this step is performed under the premise that the total length of the message is greater than the length of the text content. If the total length of the message is greater than the length of the text content, it means that the message contains a file, then the corresponding text content is extracted from the received message to be decoded according to the length of the text content, and then the text content can be converted into the desired file format according to the requirements.
[0152] Based on the contents of S201-S205 above, it can be known that the present application uses a fixed coding form to represent the total message length, text content length, text content and file content, and extracts the coded data of each part in an orderly manner according to the preset number of bits, which can effectively avoid the problems of sticking and unpacking in the data communication process. At the same time, the various components of the message are clearly defined, and are divided and extracted according to the preset number of bits, so that the decoding process is clear. In addition, the text content length is obtained by calculating the length of the second segment code, and the text content is extracted from the message to be decoded for decoding, making the parsing process more accurate and reliable.
[0153] See also Figure 3 , Figure 3 A schematic diagram of the structure of a message encoding device provided in an embodiment of the present application. Figure 3 As shown, the encoding device of the message includes:
[0154] The first acquisition unit 301 is used to acquire the file content and text content to be encoded;
[0155] A file content length calculation unit 302, used to calculate the file content length of the file content;
[0156] A preliminary first text content length calculation unit 303, used to calculate the preliminary text content length of the text content;
[0157] A first calculation unit 304, configured to calculate the total message length according to the file content length and the preliminary text content length;
[0158] A conversion unit 305 is used to convert the preliminary text content length into a data type to obtain a text content length;
[0159] The concatenation unit 306 is used to concatenate the total message length, the text content length, the text content and the file content in sequence to obtain an encoded message.
[0160] In a possible implementation, the file content length calculation unit 302 specifically includes:
[0161] A first encoding unit, used for encoding the file content into a fixed encoding form;
[0162] The second calculation unit is used to calculate the length of the file content in the fixed encoding form as the file content length.
[0163] In a possible implementation, the preliminary first text content length calculation unit 303 specifically includes:
[0164] A second encoding unit, used for encoding the text content into a fixed encoding form;
[0165] The third calculation unit is used to calculate the length of the text content in the fixed encoding form as the preliminary text content length.
[0166] In a possible implementation, the first calculating unit 304 specifically includes:
[0167] A first summing unit, used for summing the length of the file content and the length of the preliminary text content;
[0168] A second summing unit, configured to add a fixed length to the sum of the file content length and the preliminary text content length to obtain a preliminary total length;
[0169] The third encoding unit is used to encode the preliminary total length into a fixed encoding form to obtain the total length of the message; the total length of the message conforms to the first fixed-length data type.
[0170] In a possible implementation, the conversion unit 305 is specifically configured to:
[0171] The text content length is encoded into a fixed encoding form to obtain the text content length; the text content length conforms to the second fixed-length data type.
[0172] In the embodiment of the present application, the first acquisition unit 301 first acquires the file content and text content to be encoded, and calculates the file content length of the file content through the file content length calculation unit 302. Then the preliminary first text content length calculation unit 303 is used to calculate the preliminary text content length of the text content, so that the first calculation unit 304 can calculate the total message length according to the file content length and the preliminary text content length. The conversion unit 305 is then used to convert the preliminary text content length into a data type to obtain the text content length, so that the splicing unit 306 can sequentially splice the total message length, the text content length, the text content and the file content to obtain the encoded message. The present application only focuses on the length information of the message itself, reducing the verification process for the message itself and the additional protocol attribute processing process. In addition, the text content part and the file content part are clearly distinguished, and the decoupling of different information types is achieved. In the process of processing the message, the entanglement and interweaving of the text content and the file content are avoided, thereby improving the message processing efficiency.
[0173] See also Figure 4 , Figure 4 This is a schematic diagram of the structure of a message decoding device provided in an embodiment of the present application. Figure 4 As shown, the message decoding device includes:
[0174] The first segment code acquisition unit 401 is used to acquire the first code in the message to be decoded that is encoded to a first preset number of bits, and obtain the first segment code; the message to be decoded consists of the total length of the message, the length of the text content, the text content, and the file content; the total length of the message, the length of the text content, the text content, and the file content are all codes in a fixed coding form;
[0175] A message total length calculation unit 402, used to calculate the length of the first segment code to obtain the total length of the message;
[0176] The to-be-decoded message receiving unit 403 is configured to receive the to-be-decoded message according to the total length of the message;
[0177] The second segment code acquisition unit 404 is used to acquire the code from the first preset number of bits to the second preset number of bits in the message to be decoded, and obtain the second segment code;
[0178] A second text content length calculation unit 405, used to calculate the length of the second segment code to obtain the text content length;
[0179] The extraction and decoding unit 406 is used to extract the text content from the message to be decoded according to the length of the text content, and decode the text content in a fixed encoding form to obtain text content information.
[0180] In a possible implementation, the message total length calculation unit 402 specifically includes:
[0181] A first conversion unit, used to convert the first segment of code into a first character string;
[0182] The second conversion unit is used to convert the first character string into a number to obtain the total length of the message.
[0183] In a possible implementation, the second text content length calculation unit 405 specifically includes:
[0184] A third conversion unit, used for converting the second segment code into a second character string;
[0185] The fourth conversion unit is used to convert the second character string into a number to obtain the text content length.
[0186] In a possible implementation manner, the device further includes:
[0187] a calculation cache unit, for calculating the length of the received message to be decoded to obtain a received length, and placing the message to be decoded in the cache into the memory of the executable program when the message to be decoded is received according to the total length of the message and the total length of the message is greater than a preset length;
[0188] A remaining length calculation unit, used to calculate the remaining length according to the received length and the total length of the message;
[0189] A receiving unit is used to continue receiving the message to be decoded according to the remaining length until the remaining length returns to zero.
[0190] In a possible implementation, the extraction and decoding unit 406 is specifically configured to:
[0191] When the total length of the message is greater than the length of the text content, the text content is extracted from the message to be decoded according to the length of the text content, and the text content is decoded in a fixed coding form to obtain text content information.
[0192] In the embodiment of the present application, the first segment code acquisition unit 401 will first obtain the first code in the message to be decoded to the first preset number of bits to obtain the first segment code, and the message to be decoded is composed of the total length of the message, the length of the text content, the text content and the file content; the total length of the message, the length of the text content, the text content and the file content are all codes in a fixed coding form. Then the message total length calculation unit 402 can calculate the length of the first segment code to obtain the total length of the message, so that the message receiving unit 403 to be decoded can receive the message to be decoded according to the total length of the message. Next, the second segment code acquisition unit 404 will obtain the code from the first preset number of bits to the second preset number of bits in the message to be decoded, and obtain the second segment code, and then use the second text content length calculation unit 405 to calculate the length of the second segment code to obtain the text content length, so that the extraction decoding unit 406 can extract the text content from the message to be decoded according to the text content length, and decode the text content in a fixed coding form to obtain the text content information. By using a fixed coding form, the present application can make the message parsing process more standardized and stable, and reduce the errors that may occur in the parsing process. At the same time, the various components of the message are clearly defined, and they are divided and extracted according to the preset number of bits, making the decoding process clear and concise. In addition, the length of the text content is obtained by calculating the length of the second segment code, and the text content is extracted from the message to be decoded for decoding, making the parsing process more accurate and reliable.
[0193] The above is a detailed introduction to a message encoding method and a message decoding method provided by the present application. The various embodiments in the specification are described in a progressive manner, and each embodiment focuses on the differences from other embodiments. The same and similar parts between the embodiments can be referred to each other. For the device disclosed in the embodiment, since it corresponds to the method disclosed in the embodiment, the description is relatively simple, and the relevant parts can be referred to the method part description. It should be pointed out that for ordinary technicians in this technical field, without departing from the principles of the present application, several improvements and modifications can be made to the present application, and these improvements and modifications also fall within the scope of protection of the claims of the present application.
[0194] It should be understood that in the present application, "at least one (item)" means one or more, and "plurality" means two or more. "And / or" is used to describe the association relationship of associated objects, indicating that three relationships may exist. For example, "A and / or B" can mean: only A exists, only B exists, and A and B exist at the same time, where A and B can be singular or plural. The character " / " generally indicates that the objects associated before and after are in an "or" relationship. "At least one of the following" or similar expressions refers to any combination of these items, including any combination of single or plural items. For example, at least one of a, b or c can mean: a, b, c, "a and b", "a and c", "b and c", or "a and b and c", where a, b, c can be single or multiple.
[0195] It should also be noted that, in this article, relational terms such as first and second, etc. are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Moreover, the terms "include", "comprise" or any other variants thereof are intended to cover non-exclusive inclusion, so that a process, method, article or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or device. In the absence of further restrictions, the elements defined by the sentence "comprise a ..." do not exclude the presence of other identical elements in the process, method, article or device including the elements.
Claims
1. A message encoding method, characterized in that: The method comprises: Get the file content and text content to be encoded; Calculating the file content length of the file content, and calculating the preliminary text content length of the text content; Calculate the total length of the message according to the file content length and the preliminary text content length, and convert the preliminary text content length into a text content length; The total length of the message, the length of the text content, the text content and the file content are sequentially concatenated to obtain an encoded message.
2. The method according to claim 1, characterized in that The calculating the file content length of the file content includes: Encoding the file content into a fixed encoding form; The length of the file content in the fixed encoding form is calculated as the file content length.
3. The method according to claim 1, characterized in that The calculating the preliminary text content length of the text content comprises: Encoding the text content into a fixed encoding form; The length of the text content in the fixed encoding form is calculated as the preliminary text content length.
4. The method according to claim 1, characterized in that The calculating the total message length according to the file content length and the preliminary text content length comprises: Sum the length of the file content and the length of the preliminary text content; Add the sum of the file content length and the preliminary text content length to the fixed length to obtain a preliminary total length; The preliminary total length is encoded into a fixed encoding form to obtain the total length of the message; the total length of the message conforms to the first fixed-length data type.
5. The method according to claim 1, characterized in that The preliminary text content length is converted into a data type to obtain the text content length, including: The text content length is encoded into a fixed encoding form to obtain the text content length; the text content length conforms to the second fixed-length data type.
6. A method for decoding a message, characterized in that: The method comprises: Obtaining the first code in the message to be decoded that is encoded to a first preset number of bits, and obtaining a first segment of code; the message to be decoded consists of a total message length, a text content length, a text content, and a file content; the total message length, the text content length, the text content, and the file content are all codes in a fixed coding form; Calculating the length of the first segment code to obtain the total length of the message, and receiving the message to be decoded according to the total length of the message; Obtaining the code from the first preset number of bits to the second preset number of bits in the received message to be decoded, to obtain a second segment code; Calculate the length of the second segment code to obtain the length of the text content; The text content is extracted from the received message to be decoded according to the length of the text content, and the text content is decoded in a fixed encoding form to obtain text content information.
7. The method according to claim 6, characterized in that The calculating the length of the first segment code to obtain the total length of the message includes: Convert the first segment of code into a first character string; The first character string is converted into a number to obtain the total length of the message.
8. The method according to claim 6, characterized in that The calculating the length of the second segment code to obtain the text content length includes: Convert the second segment of code into a second character string; The second character string is converted into a number to obtain the length of the text content.
9. The method according to claim 6, characterized in that The method further comprises: When the message to be decoded is received according to the total length of the message and the total length of the message is greater than the preset length, the length of the received message to be decoded is calculated to obtain the received length, and the message to be decoded in the buffer area is placed in the memory of the executable program; Calculate the remaining length according to the received length and the total length of the message; The message to be decoded continues to be received according to the remaining length until the remaining length returns to zero.
10. The method according to claim 6, characterized in that The extracting the text content from the received message to be decoded according to the length of the text content, and decoding the text content in a fixed encoding form to obtain text content information, includes: When the total length of the message is greater than the length of the text content, the text content is extracted from the received message to be decoded according to the length of the text content, and the text content is decoded in a fixed coding form to obtain text content information.