C Language Implementation of BCD and Hexadecimal Conversion

1. BCD and Hexadecimal Conversion

The test code is as follows:

#include <stdio.h>
#include <string.h>
// Convert BCD to hexadecimal
unsigned char bcd_to_hex(unsigned char bcd) {
    return ((bcd >> 4) * 10) + (bcd & 0x0F);
}
// Convert hexadecimal to BCD
unsigned char hex_to_bcd(unsigned char hex) {
    return ((hex / 10) << 4) | (hex % 10);
}
// Convert BCD string to hexadecimal string
void bcd_string_to_hex_string(const unsigned char *bcd_str, unsigned char *hex_str, size_t len) {
    for (size_t i = 0; i < len; i++) {
        sprintf((char*)&hex_str[i * 2], "%02X", bcd_to_hex(bcd_str[i]));
    }
    hex_str[len * 2] = '\0';
}
// Convert hexadecimal string to BCD string
void hex_string_to_bcd_string(const unsigned char *hex_str, unsigned char *bcd_str, size_t len) {
    for (size_t i = 0; i < len; i++) {
        unsigned char hex_value;
        sscanf((const char*)&hex_str[i * 2], "%02hhX", &hex_value);
        bcd_str[i] = hex_to_bcd(hex_value);
    }
}
// Test function
int main() {
    // Test single byte conversion
    unsigned char test_value = 0x45; // BCD represents 45, hexadecimal value is 0x2D
    printf("Original BCD value: 0x%02X\n", test_value);
    printf("Converted to hexadecimal: 0x%02X\n", bcd_to_hex(test_value));
    unsigned char hex_value = 0x2D; // Hexadecimal value 45
    printf("Original hexadecimal value: 0x%02X\n", hex_value);
    printf("Converted to BCD: 0x%02X\n", hex_to_bcd(hex_value));
    // Test string conversion
    unsigned char bcd_data[] = {0x12, 0x34, 0x56, 0x78};
    unsigned char hex_result[9]; // 4 bytes BCD converted to 8 character hexadecimal string + null character
    printf("\nBCD array: ");
    for (int i = 0; i < 4; i++) {
        printf("0x%02X ", bcd_data[i]);
    }
    bcd_string_to_hex_string(bcd_data, hex_result, 4);
    printf("\nConverted to hexadecimal string: %s\n", hex_result);
    // Convert hexadecimal string back to BCD
    unsigned char bcd_result[4];
    hex_string_to_bcd_string(hex_result, bcd_result, 4);
    printf("Converted back to BCD array: ");
    for (int i = 0; i < 4; i++) {
        printf("0x%02X ", bcd_result[i]);
    }
    printf("\n");
    return 0;
}

2. Code Explanation

  1. bcd_to_hex function:

  • Converts a single BCD byte to a hexadecimal value
  • Obtains the tens digit by right-shifting 4 bits and the units digit using 0x0F
  • Calculation formula: <span>(tens digit * 10) + units digit</span>
  • hex_to_bcd function:

    • Converts a hexadecimal value to BCD
    • Obtains the tens digit by dividing by 10 and the units digit using modulo 10
    • Calculation formula: <span>(tens digit << 4) | units digit</span>
  • String conversion functions:

    • <span>bcd_string_to_hex_string</span>: Converts BCD string to hexadecimal string
    • <span>hex_string_to_bcd_string</span>: Converts hexadecimal string to BCD string

    3. Compilation and Execution

    Test results:C Language Implementation of BCD and Hexadecimal Conversion

    Leave a Comment