using System;
using System.Collections.Generic;
using System.Diagnostics;
namespace NumberSystemsDLL
{
public class NumberSystems
{
///
/// Конвертирование 10 систему в 16-ричную
///
///
///
public string Convert10To16(int numbers)
{
TextWriterTraceListener outLog = new TextWriterTraceListener("log.txt");
Trace.Listeners.Add(outLog);
Trace.AutoFlush = true;
string answer = "";
int ost = 0;
while (numbers > 0) {
ost = numbers % 16;
numbers = numbers / 16;
answer = numberToLetter[ost] + answer;
Trace.WriteLine("Trace: " + answer);
Debug.WriteLine("Debug: " + answer);
Trace.WriteIf((answer == "1"), "WriteIf = " + answer);
}
/*Debugger.Launch();*/
return answer;
}
///
/// Конвертирование 16 систему в 10-ричную
///
///
///
public int Convert16To10(string str)
{
int answer = 0;
for (int i = 0; i < str.Length; i++)
{
char ost = str[i];
int power = (str.Length - 1) - i;
answer += letterToNumber[ost] * (int) Math.Pow(16, power);
}
return answer;
}
///
/// Перевод из числа в букву
///
public Dictionary numberToLetter = new Dictionary()
{
{ 1, '1'},
{ 2, '2'},
{ 3, '3'},
{ 4, '4'},
{ 5, '5'},
{ 6, '6'},
{ 7, '7'},
{ 8, '8'},
{ 9, '9'},
{ 10, 'A'},
{ 11, 'B'},
{ 12, 'C'},
{ 13, 'D'},
{ 14, 'E'},
{ 15, 'F'}
};
///
/// Перевод из буквы в число
///
public Dictionary letterToNumber = new Dictionary()
{
{ '1', 1},
{ '2', 2},
{ '3', 3},
{ '4', 4},
{ '5', 5},
{ '6', 6},
{ '7', 7},
{ '8', 8},
{ '9', 9},
{ 'A', 10},
{ 'B', 11},
{ 'C', 12},
{ 'D', 13},
{ 'E', 14},
{ 'F', 15}
};
}
}