1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- namespace NumberSystemsDLL
- {
- public class NumberSystems
- {
- /// <summary>
- /// Конвертирование 10 систему в 16-ричную
- /// </summary>
- /// <param name="str"></param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// Конвертирование 16 систему в 10-ричную
- /// </summary>
- /// <param name="str"></param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// Перевод из числа в букву
- /// </summary>
- public Dictionary<int, char> numberToLetter = new Dictionary<int, char>()
- {
- { 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'}
- };
- /// <summary>
- /// Перевод из буквы в число
- /// </summary>
- public Dictionary<char, int> letterToNumber = new Dictionary<char, int>()
- {
- { '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}
- };
- }
- }
|