NumberSystems.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. namespace NumberSystemsDLL
  5. {
  6. public class NumberSystems
  7. {
  8. /// <summary>
  9. /// Конвертирование 10 систему в 16-ричную
  10. /// </summary>
  11. /// <param name="str"></param>
  12. /// <returns></returns>
  13. public string Convert10To16(int numbers)
  14. {
  15. TextWriterTraceListener outLog = new TextWriterTraceListener("log.txt");
  16. Trace.Listeners.Add(outLog);
  17. Trace.AutoFlush = true;
  18. string answer = "";
  19. int ost = 0;
  20. while (numbers > 0) {
  21. ost = numbers % 16;
  22. numbers = numbers / 16;
  23. answer = numberToLetter[ost] + answer;
  24. Trace.WriteLine("Trace: " + answer);
  25. Debug.WriteLine("Debug: " + answer);
  26. Trace.WriteIf((answer == "1"), "WriteIf = " + answer);
  27. }
  28. /*Debugger.Launch();*/
  29. return answer;
  30. }
  31. /// <summary>
  32. /// Конвертирование 16 систему в 10-ричную
  33. /// </summary>
  34. /// <param name="str"></param>
  35. /// <returns></returns>
  36. public int Convert16To10(string str)
  37. {
  38. int answer = 0;
  39. for (int i = 0; i < str.Length; i++)
  40. {
  41. char ost = str[i];
  42. int power = (str.Length - 1) - i;
  43. answer += letterToNumber[ost] * (int) Math.Pow(16, power);
  44. }
  45. return answer;
  46. }
  47. /// <summary>
  48. /// Перевод из числа в букву
  49. /// </summary>
  50. public Dictionary<int, char> numberToLetter = new Dictionary<int, char>()
  51. {
  52. { 1, '1'},
  53. { 2, '2'},
  54. { 3, '3'},
  55. { 4, '4'},
  56. { 5, '5'},
  57. { 6, '6'},
  58. { 7, '7'},
  59. { 8, '8'},
  60. { 9, '9'},
  61. { 10, 'A'},
  62. { 11, 'B'},
  63. { 12, 'C'},
  64. { 13, 'D'},
  65. { 14, 'E'},
  66. { 15, 'F'}
  67. };
  68. /// <summary>
  69. /// Перевод из буквы в число
  70. /// </summary>
  71. public Dictionary<char, int> letterToNumber = new Dictionary<char, int>()
  72. {
  73. { '1', 1},
  74. { '2', 2},
  75. { '3', 3},
  76. { '4', 4},
  77. { '5', 5},
  78. { '6', 6},
  79. { '7', 7},
  80. { '8', 8},
  81. { '9', 9},
  82. { 'A', 10},
  83. { 'B', 11},
  84. { 'C', 12},
  85. { 'D', 13},
  86. { 'E', 14},
  87. { 'F', 15}
  88. };
  89. }
  90. }