Base Number Conversion Algorithms and Implementation

Problem B: Arbitrary Base Conversion

This code converts a number from one arbitrary base to another.

Implementation

#include <stdio.h>
#include <string.h>

// Convert from base `src_base` string `src_num` to decimal integer.
int convert_to_decimal(int src_base, const char *src_num) {
    int result = 0;
    int place_value = 1;
    int length = strlen(src_num);
    
    for (int i = length - 1; i >= 0; i--) {
        char current_char = src_num[i];
        // Handle lowercase letters
        if (current_char >= 'a' && current_char <= 'z') {
            current_char -= 32;
        }
        // Convert character to its numeric value
        int digit_value;
        if (current_char >= 'A' && current_char <= 'Z') {
            digit_value = current_char - 'A' + 10;
        } else {
            digit_value = current_char - '0';
        }
        result += digit_value * place_value;
        place_value *= src_base;
    }
    return result;
}

// Convert decimal integer `value` to base `dst_base` and store in `output`.
// Returns the number of digits written.
int convert_from_decimal(int dst_base, int value, char *output) {
    int index = 0;
    
    do {
        int remainder = value % dst_base;
        if (remainder < 10) {
            output[index++] = remainder + '0';
        } else {
            output[index++] = remainder - 10 + 'A';
        }
        value /= dst_base;
    } while (value != 0);
    
    return index;
}

int main() {
    char input_num[40], output_digits[40];
    int source_base, target_base;
    
    while (scanf("%d %s %d", &source_base, input_num, &target_base) != EOF) {
        int decimal_value = convert_to_decimal(source_base, input_num);
        int digit_count = convert_from_decimal(target_base, decimal_value, output_digits);
        
        // Output in reverse order (most significant digit first)
        for (int i = digit_count - 1; i >= 0; i--) {
            printf("%c", output_digits[i]);
        }
        printf("\n");
    }
    return 0;
}

Problem C: Decimal to Binary Conversion for Large Numbers

This algorithm converts a very large decimal number (represented as a string) directly to its binary representation.

Algorithm Explanation

The decimal number is stored as a chraacter array. The conversion process repeatedly divides the entire decimal number by 2, recordign the remainder each time to build the binary result from least significant bit to most significant bit.

The division process works from the most significant digit to the least:

  • For each digit, perform integer division by 2.
  • If the current digit is odd, add 10 to the next digit (crarying the remainder).
  • If the current digit is even, proceed normally.

Implementation

#include <stdio.h>
#include <string.h>

int main() {
    char decimal_str[35];
    char binary_digits[200];
    
    while (scanf("%s", decimal_str) != EOF) {
        int binary_index = 0;
        int str_index = 0;
        int length = strlen(decimal_str);
        
        // Convert ASCII characters to their integer values
        for (int i = 0; i < length; i++) {
            decimal_str[i] -= '0';
        }
        
        // Perform repeated division by 2
        do {
            // Get least significant digit for binary output
            binary_digits[binary_index++] = (decimal_str[length - 1] % 2) + '0';
            
            int carry = 0;
            // Divide the entire decimal number by 2
            for (int j = str_index; j < length; j++) {
                int current_digit = decimal_str[j];
                decimal_str[j] = (current_digit + carry) / 2;
                // If current digit was odd, carry 10 to next digit
                if (current_digit % 2 == 1) {
                    carry = 10;
                } else {
                    carry = 0;
                }
            }
            // Skip leading zeros
            if (decimal_str[str_index] == 0) {
                str_index++;
            }
        } while (str_index < length);
        
        // Output binary digits in correct order (most significant first)
        for (int i = binary_index - 1; i >= 0; i--) {
            printf("%c", binary_digits[i]);
        }
        printf("\n");
    }
    
    return 0;
}

Tags: algorithm number-theory base-conversion c-programming

Posted on Mon, 18 May 2026 01:51:55 +0000 by thebluebus