How can i represent decimal negative number in octal without using Convert.To()
method?
public static string GetOctal(int number)
{
int base = 8;
}
So for example :
-3 (base 10) = 37777777775 (base 8)
But what algorithm should i use to get this result?
The are test cases for my task:
[TestCase(-3, 8, ExpectedResult = "37777777775")]
[TestCase(-127, 8, ExpectedResult = "37777777601")]
[TestCase(-675432, 8, ExpectedResult = "37775330630")]
[TestCase(-1908345, 8, ExpectedResult = "37770560607")]
[TestCase(int.MinValue, 8, ExpectedResult = "20000000000")]
[TestCase(-3, 16, ExpectedResult = "FFFFFFFD")]
[TestCase(-127, 16, ExpectedResult = "FFFFFF81")]
[TestCase(-675432, 16, ExpectedResult = "FFF5B198")]
[TestCase(-1908345, 16, ExpectedResult = "FFE2E187")]
[TestCase(int.MinValue, 16, ExpectedResult = "80000000")]
[TestCase(1908345, 10, ExpectedResult = "1908345")]
[TestCase(int.MaxValue, 10, ExpectedResult = "2147483647")]
public string GetRadix_Tests(int number, int radix) => number.GetRadix(radix);
You can always go the source code of the
Convert
class at source.dot.net here and followParseNumber
'sIntToString
method: