Advertisement
yahorrr

Untitled

Sep 26th, 2022
580
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.39 KB | None | 0 0
  1. using System;
  2. using System.IO.Pipes;
  3. using System.Text;
  4. using System.Threading;
  5.  
  6. namespace NumeralSystems
  7. {
  8.     public static class Converter
  9.     {
  10.         /// <summary>
  11.         /// Gets the value of a positive integer to its equivalent string representation in the octal numeral systems.
  12.         /// </summary>
  13.         /// <param name="number">Source number.</param>
  14.         /// <returns>The equivalent string representation of the number in the octal numeral systems.</returns>
  15.         /// <exception cref="ArgumentException">Thrown if number is less than zero.</exception>
  16.         public static string GetPositiveOctal(this int number)
  17.         {
  18.             return GetPositiveRadix(number, 8);
  19.         }
  20.  
  21.         /// <summary>
  22.         /// Gets the value of a positive integer to its equivalent string representation in the decimal numeral systems.
  23.         /// </summary>
  24.         /// <param name="number">Source number.</param>
  25.         /// <returns>The equivalent string representation of the number in the decimal numeral systems.</returns>
  26.         /// <exception cref="ArgumentException">Thrown if number is less than zero.</exception>
  27.         public static string GetPositiveDecimal(this int number)
  28.         {
  29.             return GetPositiveRadix(number, 10);
  30.         }
  31.  
  32.         /// <summary>
  33.         /// Gets the value of a positive integer to its equivalent string representation in the hexadecimal numeral systems.
  34.         /// </summary>
  35.         /// <param name="number">Source number.</param>
  36.         /// <returns>The equivalent string representation of the number in the hexadecimal numeral systems.</returns>
  37.         /// <exception cref="ArgumentException">Thrown if number is less than zero.</exception>
  38.         public static string GetPositiveHex(this int number)
  39.         {
  40.             return GetPositiveRadix(number, 16);
  41.         }
  42.  
  43.         /// <summary>
  44.         /// Gets the value of a positive integer to its equivalent string representation in a specified radix.
  45.         /// </summary>
  46.         /// <param name="number">Source number.</param>
  47.         /// <param name="radix">Base of the numeral systems.</param>
  48.         /// <returns>The equivalent string representation of the number in a specified radix.</returns>
  49.         /// <exception cref="ArgumentException">Thrown if radix is not equal 8, 10 or 16.</exception>
  50.         /// <exception cref="ArgumentException">Thrown if number is less than zero.</exception>
  51.         public static string GetPositiveRadix(this int number, int radix)
  52.         {
  53.             if (number < 0)
  54.             {
  55.                 throw new ArgumentException("number is must be more than zero", nameof(number));
  56.             }
  57.  
  58.             return GetRadix(number, radix);
  59.         }
  60.  
  61.         /// <summary>
  62.         /// Gets the value of a signed integer to its equivalent string representation in a specified radix.
  63.         /// </summary>
  64.         /// <param name="number">Source number.</param>
  65.         /// <param name="radix">Base of the numeral systems.</param>
  66.         /// <returns>The equivalent string representation of the number in a specified radix.</returns>
  67.         /// <exception cref="ArgumentException">Thrown if radix is not equal 8, 10 or 16.</exception>
  68.         public static string GetRadix(this int number, int radix)
  69.         {
  70.             if (radix != 8 && radix != 10 && radix != 16)
  71.             {
  72.                 throw new ArgumentException("radix is must be equal 8, 10 or 16", nameof(radix));
  73.             }
  74.  
  75.             StringBuilder result = new StringBuilder();
  76.  
  77.             uint uNumber = (uint)number;
  78.             uint uRadix = (uint)radix;
  79.  
  80.             while (uNumber != 0)
  81.             {
  82.                 result.Insert(0, GetDigit(uNumber % uRadix));
  83.                 uNumber /= uRadix;
  84.             }
  85.  
  86.             return result.ToString();
  87.         }
  88.  
  89.         private static string GetDigit(uint remainder) =>
  90.             remainder switch
  91.             {
  92.                 0 => "0",
  93.                 1 => "1",
  94.                 2 => "2",
  95.                 3 => "3",
  96.                 4 => "4",
  97.                 5 => "5",
  98.                 6 => "6",
  99.                 7 => "7",
  100.                 8 => "8",
  101.                 9 => "9",
  102.                 10 => "A",
  103.                 11 => "B",
  104.                 12 => "C",
  105.                 13 => "D",
  106.                 14 => "E",
  107.                 15 => "F",
  108.                 _ => null
  109.             };
  110.     }
  111. }
  112.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement