# artemkorsakov's solution

## to All Your Base in the C# Track

Published at Feb 11 2019 · 0 comments
Instructions
Test suite
Solution

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.

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!

## Running the tests

To run the tests, run the command `dotnet test` from within the exercise directory.

Initially, only the first test will be enabled. This is to encourage you to solve the exercise one step at a time. Once you get the first test passing, remove the `Skip` property from the next test and work on getting that test passing. Once none of the tests are skipped and they are all passing, you can submit your solution using `exercism submit AllYourBase.cs`

## Further information

For more detailed information about the C# track, including how to get help if you're having trouble, please visit the exercism.io C# language page.

## Submitting Incomplete Solutions

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

### AllYourBaseTest.cs

``````// This file was auto-generated based on version 2.3.0 of the canonical data.

using System;
using Xunit;

public class AllYourBaseTest
{
[Fact]
public void Single_bit_one_to_decimal()
{
var inputBase = 2;
var digits = new[] { 1 };
var outputBase = 10;
var expected = new[] { 1 };
Assert.Equal(expected, AllYourBase.Rebase(inputBase, digits, outputBase));
}

[Fact(Skip = "Remove to run test")]
public void Binary_to_single_decimal()
{
var inputBase = 2;
var digits = new[] { 1, 0, 1 };
var outputBase = 10;
var expected = new[] { 5 };
Assert.Equal(expected, AllYourBase.Rebase(inputBase, digits, outputBase));
}

[Fact(Skip = "Remove to run test")]
public void Single_decimal_to_binary()
{
var inputBase = 10;
var digits = new[] { 5 };
var outputBase = 2;
var expected = new[] { 1, 0, 1 };
Assert.Equal(expected, AllYourBase.Rebase(inputBase, digits, outputBase));
}

[Fact(Skip = "Remove to run test")]
public void Binary_to_multiple_decimal()
{
var inputBase = 2;
var digits = new[] { 1, 0, 1, 0, 1, 0 };
var outputBase = 10;
var expected = new[] { 4, 2 };
Assert.Equal(expected, AllYourBase.Rebase(inputBase, digits, outputBase));
}

[Fact(Skip = "Remove to run test")]
public void Decimal_to_binary()
{
var inputBase = 10;
var digits = new[] { 4, 2 };
var outputBase = 2;
var expected = new[] { 1, 0, 1, 0, 1, 0 };
Assert.Equal(expected, AllYourBase.Rebase(inputBase, digits, outputBase));
}

[Fact(Skip = "Remove to run test")]
{
var inputBase = 3;
var digits = new[] { 1, 1, 2, 0 };
var outputBase = 16;
var expected = new[] { 2, 10 };
Assert.Equal(expected, AllYourBase.Rebase(inputBase, digits, outputBase));
}

[Fact(Skip = "Remove to run test")]
{
var inputBase = 16;
var digits = new[] { 2, 10 };
var outputBase = 3;
var expected = new[] { 1, 1, 2, 0 };
Assert.Equal(expected, AllYourBase.Rebase(inputBase, digits, outputBase));
}

[Fact(Skip = "Remove to run test")]
public void Number_15_bit_integer()
{
var inputBase = 97;
var digits = new[] { 3, 46, 60 };
var outputBase = 73;
var expected = new[] { 6, 10, 45 };
Assert.Equal(expected, AllYourBase.Rebase(inputBase, digits, outputBase));
}

[Fact(Skip = "Remove to run test")]
public void Empty_list()
{
var inputBase = 2;
var digits = Array.Empty<int>();
var outputBase = 10;
var expected = new[] { 0 };
Assert.Equal(expected, AllYourBase.Rebase(inputBase, digits, outputBase));
}

[Fact(Skip = "Remove to run test")]
public void Single_zero()
{
var inputBase = 10;
var digits = new[] { 0 };
var outputBase = 2;
var expected = new[] { 0 };
Assert.Equal(expected, AllYourBase.Rebase(inputBase, digits, outputBase));
}

[Fact(Skip = "Remove to run test")]
public void Multiple_zeros()
{
var inputBase = 10;
var digits = new[] { 0, 0, 0 };
var outputBase = 2;
var expected = new[] { 0 };
Assert.Equal(expected, AllYourBase.Rebase(inputBase, digits, outputBase));
}

[Fact(Skip = "Remove to run test")]
{
var inputBase = 7;
var digits = new[] { 0, 6, 0 };
var outputBase = 10;
var expected = new[] { 4, 2 };
Assert.Equal(expected, AllYourBase.Rebase(inputBase, digits, outputBase));
}

[Fact(Skip = "Remove to run test")]
public void Input_base_is_one()
{
var inputBase = 1;
var digits = new[] { 0 };
var outputBase = 10;
Assert.Throws<ArgumentException>(() => AllYourBase.Rebase(inputBase, digits, outputBase));
}

[Fact(Skip = "Remove to run test")]
public void Input_base_is_zero()
{
var inputBase = 0;
var digits = Array.Empty<int>();
var outputBase = 10;
Assert.Throws<ArgumentException>(() => AllYourBase.Rebase(inputBase, digits, outputBase));
}

[Fact(Skip = "Remove to run test")]
public void Input_base_is_negative()
{
var inputBase = -2;
var digits = new[] { 1 };
var outputBase = 10;
Assert.Throws<ArgumentException>(() => AllYourBase.Rebase(inputBase, digits, outputBase));
}

[Fact(Skip = "Remove to run test")]
public void Negative_digit()
{
var inputBase = 2;
var digits = new[] { 1, -1, 1, 0, 1, 0 };
var outputBase = 10;
Assert.Throws<ArgumentException>(() => AllYourBase.Rebase(inputBase, digits, outputBase));
}

[Fact(Skip = "Remove to run test")]
public void Invalid_positive_digit()
{
var inputBase = 2;
var digits = new[] { 1, 2, 1, 0, 1, 0 };
var outputBase = 10;
Assert.Throws<ArgumentException>(() => AllYourBase.Rebase(inputBase, digits, outputBase));
}

[Fact(Skip = "Remove to run test")]
public void Output_base_is_one()
{
var inputBase = 2;
var digits = new[] { 1, 0, 1, 0, 1, 0 };
var outputBase = 1;
Assert.Throws<ArgumentException>(() => AllYourBase.Rebase(inputBase, digits, outputBase));
}

[Fact(Skip = "Remove to run test")]
public void Output_base_is_zero()
{
var inputBase = 10;
var digits = new[] { 7 };
var outputBase = 0;
Assert.Throws<ArgumentException>(() => AllYourBase.Rebase(inputBase, digits, outputBase));
}

[Fact(Skip = "Remove to run test")]
public void Output_base_is_negative()
{
var inputBase = 2;
var digits = new[] { 1 };
var outputBase = -7;
Assert.Throws<ArgumentException>(() => AllYourBase.Rebase(inputBase, digits, outputBase));
}

[Fact(Skip = "Remove to run test")]
public void Both_bases_are_negative()
{
var inputBase = -2;
var digits = new[] { 1 };
var outputBase = -7;
Assert.Throws<ArgumentException>(() => AllYourBase.Rebase(inputBase, digits, outputBase));
}
}``````
``````﻿using System;
using System.Collections.Generic;
using System.Linq;

public static class AllYourBase
{
public static int[] Rebase(int inputBase, int[] inputDigits, int outputBase)
{
CheckArguments(inputBase, inputDigits, outputBase);
var decimalNumber = inputDigits.Select((t, i) => t * (int)Math.Pow(inputBase, inputDigits.Length - 1 - i)).Sum();

var outputDigits = new List<int>();
while (decimalNumber >= outputBase)
{
decimalNumber = decimalNumber / outputBase;
}
outputDigits.Reverse();
return outputDigits.ToArray();
}

private static void CheckArguments(int inputBase, int[] inputDigits, int outputBase)
{
if (inputBase < 2 || outputBase < 2)
{
throw new ArgumentException("inputBase and outputBase must be more than 1");
}

if (inputDigits.Any(id => id < 0) || inputDigits.Any(id => id >= inputBase))
{
throw new ArgumentException("inputDigits must be more than -1 and less than inputBase");
}
}
}``````