🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of vlzware

vlzware's solution

to All Your Base in the C Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

Convert a number, represented as a sequence of digits in one base, to any other base.

Implement general base conversion. Given a number in base a, represented as a sequence of digits, convert it to base b.

Note

  • Try to implement the conversion yourself. Do not use something else to perform the conversion for you.

About Positional Notation

In positional notation, a number in base b can be understood as a linear combination of powers of b.

The number 42, in base 10, means:

(4 * 10^1) + (2 * 10^0)

The number 101010, in base 2, means:

(1 * 2^5) + (0 * 2^4) + (1 * 2^3) + (0 * 2^2) + (1 * 2^1) + (0 * 2^0)

The number 1120, in base 3, means:

(1 * 3^3) + (1 * 3^2) + (2 * 3^1) + (0 * 3^0)

I think you got the idea!

Yes. Those three numbers above are exactly the same. Congratulations!

Getting Started

Make sure you have read the C page on the Exercism site. This covers the basic information on setting up the development environment expected by the exercises.

Passing the Tests

Get the first test compiling, linking and passing by following the three rules of test-driven development.

The included makefile can be used to create and run the tests using the test task.

make test

Create just the functions you need to satisfy any compiler errors and get the test to fail. Then write just enough code to get the test to pass. Once you've done that, move onto the next test.

As you progress through the tests, take the time to refactor your implementation for readability and expressiveness and then go on to the next test.

Try to use standard C99 facilities in preference to writing your own low-level algorithms or facilities by hand.

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

test_all_your_base.c

#include "vendor/unity.h"
#include <stddef.h>
#include <stdint.h>
#include "../src/all_your_base.h"

#include <stdio.h>

#define LENGTH(A) (sizeof(A)/sizeof(A[0]))

void setUp(void)
{
}

void tearDown(void)
{
}

void copy_array(int8_t src[], int8_t dest[DIGITS_ARRAY_SIZE], size_t n)
{
   if (n > DIGITS_ARRAY_SIZE)
      return;
   for (size_t i = 0; i < n; ++i)
      dest[i] = src[i];
}

void test_rebase(int16_t input_base, int8_t input_digits[],
                 size_t input_length, int16_t output_base,
                 int8_t expected_digits[], size_t expected_length)
{
   int8_t digits[DIGITS_ARRAY_SIZE] = { 0 };
   int8_t ex_digits[DIGITS_ARRAY_SIZE] = { 0 };

   copy_array(input_digits, digits, input_length);
   copy_array(expected_digits, ex_digits, expected_length);

   size_t actual_length = rebase(digits, input_base, output_base, input_length);

   TEST_ASSERT_EQUAL_INT32(expected_length, actual_length);

   if (expected_length > 0)
      TEST_ASSERT_EQUAL_INT8_ARRAY(ex_digits, digits, expected_length);
}

void test_single_bit_to_decimal(void)
{
   int8_t input[] = { 1 };
   int8_t expected[] = { 1 };
   test_rebase(2, input, LENGTH(input), 10, expected, LENGTH(expected));
}

void test_binary_to_single_decimal(void)
{
   TEST_IGNORE();               // delete this line to run test
   int8_t input[] = { 1, 0, 1 };
   int8_t expected[] = { 5 };
   test_rebase(2, input, LENGTH(input), 10, expected, LENGTH(expected));
}

void test_single_decimal_to_binary(void)
{
   TEST_IGNORE();
   int8_t input[] = { 5 };
   int8_t expected[] = { 1, 0, 1 };
   test_rebase(10, input, LENGTH(input), 2, expected, LENGTH(expected));
}

void test_binary_to_multiple_decimal(void)
{
   TEST_IGNORE();
   int8_t input[] = { 1, 0, 1, 0, 1, 0 };
   int8_t expected[] = { 4, 2 };
   test_rebase(2, input, LENGTH(input), 10, expected, LENGTH(expected));
}

void test_decimal_to_binary(void)
{
   TEST_IGNORE();
   int8_t input[] = { 4, 2 };
   int8_t expected[] = { 1, 0, 1, 0, 1, 0 };
   test_rebase(10, input, LENGTH(input), 2, expected, LENGTH(expected));
}

void test_trinary_to_hex(void)
{
   TEST_IGNORE();
   int8_t input[] = { 1, 1, 2, 0 };
   int8_t expected[] = { 2, 10 };
   test_rebase(3, input, LENGTH(input), 16, expected, LENGTH(expected));
}

void test_hex_to_trinary(void)
{
   TEST_IGNORE();
   int8_t input[] = { 2, 10 };
   int8_t expected[] = { 1, 1, 2, 0 };
   test_rebase(16, input, LENGTH(input), 3, expected, LENGTH(expected));
}

void test_15_bit_integer(void)
{
   TEST_IGNORE();
   int8_t input[] = { 3, 46, 60 };
   int8_t expected[] = { 6, 10, 45 };
   test_rebase(97, input, LENGTH(input), 73, expected, LENGTH(expected));
}

void test_single_zero(void)
{
   TEST_IGNORE();
   int8_t input[] = { 0 };
   int8_t expected[] = { 0 };
   test_rebase(2, input, LENGTH(input), 10, expected, 0);
}

void test_multiple_zeros(void)
{
   TEST_IGNORE();
   int8_t input[] = { 0, 0, 0 };
   int8_t expected[] = { 0 };
   test_rebase(10, input, LENGTH(input), 2, expected, 0);
}

void test_leading_zeros(void)
{
   TEST_IGNORE();
   int8_t input[] = { 0, 6, 0 };
   int8_t expected[] = { 0 };
   test_rebase(7, input, LENGTH(input), 10, expected, 0);
}

void test_first_base_is_one(void)
{
   TEST_IGNORE();
   int8_t input[] = { 0 };
   int8_t expected[] = { 0 };
   test_rebase(1, input, LENGTH(input), 10, expected, 0);
}

void test_first_base_is_zero(void)
{
   TEST_IGNORE();
   int8_t input[] = { 0 };
   int8_t expected[] = { 0 };
   test_rebase(0, input, LENGTH(input), 10, expected, 0);
}

void test_first_base_is_negative(void)
{
   TEST_IGNORE();
   int8_t input[] = { 1 };
   int8_t expected[] = { 0 };
   test_rebase(-2, input, LENGTH(input), 10, expected, 0);
}

void test_negative_digit(void)
{
   TEST_IGNORE();
   int8_t input[] = { 1, -1, 1, 0, 1, 0 };
   int8_t expected[] = { 0 };
   test_rebase(2, input, LENGTH(input), 10, expected, 0);
}

void test_invalid_positive_digit(void)
{
   TEST_IGNORE();
   int8_t input[] = { 1, 2, 1, 0, 1, 0 };
   int8_t expected[] = { 0 };
   test_rebase(2, input, LENGTH(input), 10, expected, 0);
}

void test_second_base_is_one(void)
{
   TEST_IGNORE();
   int8_t input[] = { 1, 0, 1, 0, 1, 0 };
   int8_t expected[] = { 0 };
   test_rebase(2, input, LENGTH(input), 1, expected, 0);
}

void test_second_base_is_zero(void)
{
   TEST_IGNORE();
   int8_t input[] = { 7 };
   int8_t expected[] = { 0 };
   test_rebase(10, input, LENGTH(input), 0, expected, 0);
}

void test_second_base_is_negative(void)
{
   TEST_IGNORE();
   int8_t input[] = { 1 };
   int8_t expected[] = { 0 };
   test_rebase(2, input, LENGTH(input), -7, expected, 0);
}

void test_both_bases_are_negative(void)
{
   TEST_IGNORE();
   int8_t input[] = { 1 };
   int8_t expected[] = { 0 };
   test_rebase(-2, input, LENGTH(input), -7, expected, 0);
}

int main(void)
{
   UnityBegin("test/test_all_your_base.c");

   RUN_TEST(test_single_bit_to_decimal);
   RUN_TEST(test_binary_to_single_decimal);
   RUN_TEST(test_single_decimal_to_binary);
   RUN_TEST(test_binary_to_multiple_decimal);
   RUN_TEST(test_trinary_to_hex);
   RUN_TEST(test_hex_to_trinary);
   RUN_TEST(test_15_bit_integer);
   RUN_TEST(test_single_zero);
   RUN_TEST(test_multiple_zeros);
   RUN_TEST(test_leading_zeros);
   RUN_TEST(test_first_base_is_one);
   RUN_TEST(test_first_base_is_zero);
   RUN_TEST(test_first_base_is_negative);
   RUN_TEST(test_negative_digit);
   RUN_TEST(test_invalid_positive_digit);
   RUN_TEST(test_second_base_is_one);
   RUN_TEST(test_second_base_is_zero);
   RUN_TEST(test_second_base_is_negative);
   RUN_TEST(test_both_bases_are_negative);

   UnityEnd();
   return 0;
}

makefile

CFLAGS  = -std=c99
CFLAGS += -g
CFLAGS += -Wall
CFLAGS += -Wextra
CFLAGS += -pedantic
CFLAGS += -Werror

VFLAGS  = --quiet
VFLAGS += --tool=memcheck
VFLAGS += --leak-check=full
VFLAGS += --error-exitcode=1

test: tests.out
	@./tests.out

memcheck: tests.out
	@valgrind $(VFLAGS) ./tests.out
	@echo "Memory check passed"

clean:
	rm -rf *.o *.out *.out.dSYM

tests.out: test/test_all_your_base.c src/all_your_base.c src/all_your_base.h
	@echo Compiling $@
	@cc $(CFLAGS) src/all_your_base.c test/vendor/unity.c test/test_all_your_base.c -o tests.out -lm

src/all_your_base.c

#include "all_your_base.h"
#include <math.h>
#include <stdio.h>

#define TESTS_CONDITIONS

void zeros(int8_t digits[], int start, int len);
int todec(int8_t digits[], int16_t input_base, size_t input_length,
	   int start);
size_t convert(int8_t digits[], int16_t output_base, int dec);


size_t rebase(int8_t digits[], int16_t input_base, int16_t output_base,
	      size_t input_length)
{
	/* input check */
	if (
		digits == NULL || input_base <= 1 ||
		output_base <= 1 || input_length <= 0 ||
		input_length >= DIGITS_ARRAY_SIZE
	)
		return 0;


	/* consume leading zeros - how I think it should be */
	int start = 0;
	while (digits[start] == 0)
		start++;

	/* check for leading zeros - as per the tests */
	#ifdef TESTS_CONDITIONS
	if (digits[0] == 0)
		return 0;
	#endif

	/* convert do tecimal */
	int dec = todec(digits, input_base, input_length, start);

	/* convert to output_base */
	size_t res = convert(digits, output_base, dec);
	if (dec == 0)
		return 0;

	/* zero the rest of digits */
	zeros(digits, res, DIGITS_ARRAY_SIZE);

	return res;
}


void zeros(int8_t digits[], int start, int len)
{
	int i;
	for (i = start; i < len; i++)
		digits[i] = 0;
}

int todec(int8_t digits[], int16_t input_base, size_t input_length,
	   int start)
{
	int i, dec = 0;
	for (i = start; i < (int) input_length; i++) {
		if (digits[i] < 0 || digits[i] >= input_base)
			return 0;
		dec += pow(input_base, (input_length - i - 1)) * digits[i];
	}
	return dec;
}

size_t convert(int8_t digits[], int16_t output_base, int dec)
{
	size_t res = 0;

	/* count length of output */
	int q = dec;
	while (q) {
		q /= output_base;
		res++;
	}
	if (!(res < DIGITS_ARRAY_SIZE))
		return 0;

	/* build the output counting for endianness */
	int len = res;
	while (dec) {
		len--;
		digits[len] = dec % output_base;
		dec /= output_base;
	}
	return res;
}

src/all_your_base.h

#ifndef ALL_YOUR_BASE_H
#define ALL_YOUR_BASE_H

#define DIGITS_ARRAY_SIZE 64

#include <stddef.h>
#include <stdint.h>

size_t rebase(int8_t digits[], int16_t input_base, int16_t output_base,
	      size_t input_length);

#endif

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?