fork(2) download
  1. using System;
  2. using System.Collections;
  3. using System.Text;
  4. using System.Text.RegularExpressions;
  5.  
  6. /*
  7. DECIMAL-BINARY INTERCONVERTER
  8. BENEDICT W. HAZEL
  9.  
  10. This program interconverts between binary and decimal numbers. To use
  11. it enter a calculation mode and number on two separate lines into
  12. the stdin text field, e.g.
  13. [mode]
  14. [number]
  15. For the mode use:
  16. bin - decimal -> binary
  17. dec - binary -> decimal
  18. The possible range for both modes is 0 to 65535 due to data type
  19. restrictions.
  20. */
  21.  
  22. /// <summary>
  23. /// Binary to decimal interconverter
  24. /// </summary>
  25. public class Numerics
  26. {
  27. /// <summary>
  28. /// Converts a decimal number to binary.
  29. /// </summary>
  30. /// <param name="decimalNumber">The decimal number to convert.</param>
  31. /// <returns>The binary representation of the decimal number.</returns>
  32. public static string ToBinary(uint decimalNumber)
  33. {
  34. StringBuilder binNumBuilder = new StringBuilder();
  35. uint divisionTotal = decimalNumber;
  36. byte remainder = 0;
  37. while (divisionTotal >= 1)
  38. {
  39. remainder = (byte)(divisionTotal % 2);
  40. binNumBuilder.Insert(0, remainder);
  41. divisionTotal /= 2;
  42. }
  43.  
  44. return binNumBuilder.ToString();
  45. }
  46.  
  47. /// <summary>
  48. /// Convertsa binary number to decimal.
  49. /// </summary>
  50. /// <param name="binaryNumber">The binary number to convert.</param>
  51. /// <returns>The decimal representation of the binary number.</returns>
  52. public static uint FromBinary(string binaryNumber)
  53. {
  54. BitArray bits = GetBits(binaryNumber);
  55. uint decimalNumber = 0;
  56. for (int i = 1; i <= bits.Length; i++)
  57. {
  58. if (bits[i - 1] == true)
  59. {
  60. decimalNumber += (uint)Math.Pow(2, binaryNumber.Length - i);
  61. }
  62. }
  63.  
  64. return decimalNumber;
  65. }
  66.  
  67. /// <summary>
  68. /// Converts a string representation of a binary number into a <see cref="BitArray" />.
  69. ///</summary>
  70. /// <param name="binaryNumber">The string representation of a binary number.</param>
  71. /// <returns>The binary number as a <see cref="BitArray" />.
  72. public static BitArray GetBits (string binaryNumber)
  73. {
  74. BitArray bits = new BitArray(binaryNumber.Length);
  75. byte boolConversion = 0;
  76. for (int i = 0; i < bits.Length; i++)
  77. {
  78. boolConversion = byte.Parse(binaryNumber[i].ToString());
  79. bits[i] = Convert.ToBoolean(boolConversion);
  80. }
  81.  
  82. return bits;
  83. }
  84. }
  85.  
  86. /// <summary>
  87. /// Main program.
  88. /// </summary>
  89. public class Program
  90. {
  91. /// <summary>
  92. /// Main program code.
  93. /// </summary>
  94. public static void Main(string[] args)
  95. {
  96. string calculationMode = Console.ReadLine();
  97. string inputNumber = Console.ReadLine();
  98.  
  99. if (calculationMode == "bin")
  100. {
  101. uint uintConversionOutput = 0;
  102. if (uint.TryParse(inputNumber, out uintConversionOutput))
  103. {
  104. string binaryNumber = Numerics.ToBinary(uint.Parse(inputNumber));
  105. Console.WriteLine(binaryNumber);
  106. }
  107. else
  108. {
  109. WriteError(inputNumber);
  110. }
  111. }
  112. else if (calculationMode == "dec")
  113. {
  114. if (Regex.IsMatch(inputNumber, "^[01]*$"))
  115. {
  116. uint decimalNumber = Numerics.FromBinary(inputNumber);
  117. Console.WriteLine(decimalNumber);
  118. }
  119. else
  120. {
  121. WriteError(inputNumber);
  122. }
  123. }
  124. else
  125. {
  126. WriteError(calculationMode);
  127. }
  128. }
  129.  
  130. /// <summary>
  131. /// Writes an error message to the console.
  132. /// </summary>
  133. /// <param name="input">The input causing the error.</param>
  134. public static void WriteError(string input)
  135. {
  136. Console.WriteLine("An error occurred when processing {0}.", input);
  137. }
  138. }
Success #stdin #stdout 0.08s 34240KB
stdin
dec
10001001
stdout
137