SoFunction
Updated on 2025-03-03

C/C++ implements the conversion of 7bit and 8bit encodings to each other

1. Convert 8bit encoding to 7bit encoding

#include <>
#include <>
#include <>

// Function declaration: Convert 8bit encoding to 7bit encodingunsigned char* encode_8bit_to_7bit(const unsigned char* input, size_t input_len, size_t* output_len);

int main() {
    const char* input_str = "hello";  // Entered string    size_t input_len = strlen(input_str);  // Enter the length of the string    size_t output_len = 0;  // The output length after 7bit encoding
    // Call the conversion function    unsigned char* encoded_data = encode_8bit_to_7bit((const unsigned char*)input_str, input_len, &output_len);

    // Print the 7bit encoding result    printf("7-bit encoded data: ");
    for (size_t i = 0; i < output_len; i++) {
        printf("%02X ", encoded_data[i]);
    }
    printf("\n");

    // Free the allocated memory    free(encoded_data);

    return 0;
}

// Function implementation that converts 8bit encoding to 7bit encodingunsigned char* encode_8bit_to_7bit(const unsigned char* input, size_t input_len, size_t* output_len) {
    // Calculate the length after 7bit encoding    *output_len = (input_len * 7 + 7) / 8;  // Compress every 7bit characters to 8bit units    unsigned char* output = (unsigned char*)malloc(*output_len);
    if (!output) {
        fprintf(stderr, "Memory allocation failed\n");
        exit(1);
    }

    int bit_position = 0;  // Current bit position    unsigned int current_byte = 0;  // The byte currently being processed
    size_t output_index = 0;
    for (size_t i = 0; i < input_len; i++) {
        // Combine the 7-bit part of the current character with the remaining bits before        current_byte |= (input[i] & 0x7F) << bit_position;  // Get 7-bit and shift        bit_position += 7;  // Update bit position
        //Write output when there are enough 8 bits        while (bit_position >= 8) {
            output[output_index++] = current_byte & 0xFF;  // Extract the complete 8-bit            current_byte >>= 8;  // Move out the processed 8 bits            bit_position -= 8;  // Update bit position        }
    }

    // Handle the remaining bits    if (bit_position > 0) {
        output[output_index++] = current_byte & 0xFF;  //Storage the remaining part    }

    return output;
}

Code description

encode_8bit_to_7bit function:

The input to this function is a byte array (assuming each byte is an 8-bit encoded ASCII character), and the output is to compress these 8-bit encoded byte arrays.

Through bit operations, each 7-bit character is tightly packed into an 8-bit unit.

Core steps

current_byte: Used to store the currently processed bytes.

bit_position: Indicates the number of bits that have been filled.

Process the input bytes one by one, extract the lower 7 bits, and put them into current_byte through shift operations.

Whenever bit_position is greater than or equal to 8, 8 bits in current_byte are written to the output array.

Finally, if there are still remaining bits, write them to the output.

Memory management

malloc: Dynamically allocate memory to store output byte arrays.

free: After using the encoded data, free allocated memory to avoid memory leakage.

Output format

The output is a 7-bit encoded byte array, you can use printf to print the hexadecimal format of each byte.

7-bit encoded data: 68 65 6C 6C 6F 

2. The principle of converting 7-bit back to 8-bit

When 7-bit encoding is compressed, each character takes 7 bits and they are compactly packed in an 8-bit unit. When decoding this data back to 8-bit, we need to extract the corresponding characters from each byte in a 7-bit way and reconstruct the complete 8-bit encoding.

#include <>
#include <>
#include <>

// Function declaration: Convert 7bit encoding to 8bit encodingunsigned char* decode_7bit_to_8bit(const unsigned char* input, size_t input_len, size_t* output_len);

int main() {
    // Example 7bit encoded data (assuming we have encoded "hello" into 7bit format)    unsigned char encoded_data[] = {0x68, 0x65, 0x6C, 0x6C, 0x6F};  // 7bit encoding of "hello"    size_t input_len = sizeof(encoded_data);  // Enter the length of 7bit data    size_t output_len = 0;  // The decoded 8bit data length
    // Call the decoding function    unsigned char* decoded_data = decode_7bit_to_8bit(encoded_data, input_len, &output_len);

    // Print the decoded 8bit characters    printf("Decoded 8-bit data: ");
    for (size_t i = 0; i < output_len; i++) {
        printf("%c", decoded_data[i]);
    }
    printf("\n");

    // Free the allocated memory    free(decoded_data);

    return 0;
}

// Function implementation that converts 7bit encoding to 8bit encodingunsigned char* decode_7bit_to_8bit(const unsigned char* input, size_t input_len, size_t* output_len) {
    // Calculate the length after 8bit decode    *output_len = (input_len * 8) / 7;  // Restore to 8bit characters every 7bit    unsigned char* output = (unsigned char*)malloc(*output_len);
    if (!output) {
        fprintf(stderr, "Memory allocation failed\n");
        exit(1);
    }

    int bit_position = 0;  // Current position    unsigned int current_byte = 0;  // The byte currently being processed
    size_t output_index = 0;
    for (size_t i = 0; i < input_len; i++) {
        current_byte |= (input[i] & 0xFF) << bit_position;  // Add the current byte to current_byte        bit_position += 8;  // Add 8 digits each time
        // Extract every 7bit characters until less than 7bits        while (bit_position >= 7) {
            output[output_index++] = current_byte & 0x7F;  // Extract 7bit characters            current_byte >>= 7;  // Remove the extracted 7bits            bit_position -= 7;  // Update bit position        }
    }

    return output;
}

Code description

decode_7bit_to_8bit function:

Input: This function receives a 7-bit compressed encoded data array and decodes it back to bytes in 8-bit format.

Output: Return an 8-bit decoded byte array, restored to the complete ASCII encoding.

How it works

current_byte: Used to store bits spliced ​​from input data.

bit_position: Record the number of bits that have been spliced.

Process the input bytes one by one, read them in an 8-bit manner, and extract 7-bit data from them and restore them to the original characters.

Whenever enough 7 bits are accumulated, the characters are extracted and written to the output array.

Memory management

malloc: Dynamically allocate memory to store output byte arrays.

free: After using the encoded data, free allocated memory to avoid memory leakage.

Output format

The output array is a decoded 8-bit encoded byte stream (ASCII characters)

Sample output

Suppose you provide 7-bit encoded hello data (7-bit compressed data: 0x68, 0x65, 0x6C, 0x6C, 0x6F), and the original "hello" string will be restored after decoding.

The output will be:

Decoded 8-bit data: hello

This is the article about C/C++'s implementation of 7bit and 8bit encoding conversion. For more related C++ encoding conversion content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!