Advertisement
yahorrr

Untitled

Sep 16th, 2022
728
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.71 KB | None | 0 0
  1. using System;
  2. using System.IO.Pipes;
  3. using System.Threading;
  4.  
  5. namespace NumeralSystems
  6. {
  7.     public static class Converter
  8.     {
  9.         /// <summary>
  10.         /// Gets the value of a positive integer to its equivalent string representation in the octal numeral systems.
  11.         /// </summary>
  12.         /// <param name="number">Source number.</param>
  13.         /// <returns>The equivalent string representation of the number in the octal numeral systems.</returns>
  14.         /// <exception cref="ArgumentException">Thrown if number is less than zero.</exception>
  15.         public static string GetPositiveOctal(this int number)
  16.         {
  17.             if (number < 0)
  18.             {
  19.                 throw new ArgumentException("number is less than zero", nameof(number));
  20.             }
  21.  
  22.             return GetRadix(number, 8);
  23.         }
  24.  
  25.         /// <summary>
  26.         /// Gets the value of a positive integer to its equivalent string representation in the decimal numeral systems.
  27.         /// </summary>
  28.         /// <param name="number">Source number.</param>
  29.         /// <returns>The equivalent string representation of the number in the decimal numeral systems.</returns>
  30.         /// <exception cref="ArgumentException">Thrown if number is less than zero.</exception>
  31.         public static string GetPositiveDecimal(this int number)
  32.         {
  33.             if (number < 0)
  34.             {
  35.                 throw new ArgumentException("number is less than zero", nameof(number));
  36.             }
  37.  
  38.             return GetRadix(number, 10);
  39.         }
  40.  
  41.         /// <summary>
  42.         /// Gets the value of a positive integer to its equivalent string representation in the hexadecimal numeral systems.
  43.         /// </summary>
  44.         /// <param name="number">Source number.</param>
  45.         /// <returns>The equivalent string representation of the number in the hexadecimal numeral systems.</returns>
  46.         /// <exception cref="ArgumentException">Thrown if number is less than zero.</exception>
  47.         public static string GetPositiveHex(this int number)
  48.         {
  49.             if (number < 0)
  50.             {
  51.                 throw new ArgumentException("number is less than zero", nameof(number));
  52.             }
  53.  
  54.             return GetRadix(number, 16);
  55.         }
  56.  
  57.         /// <summary>
  58.         /// Gets the value of a positive integer to its equivalent string representation in a specified radix.
  59.         /// </summary>
  60.         /// <param name="number">Source number.</param>
  61.         /// <param name="radix">Base of the numeral systems.</param>
  62.         /// <returns>The equivalent string representation of the number in a specified radix.</returns>
  63.         /// <exception cref="ArgumentException">Thrown if radix is not equal 8, 10 or 16.</exception>
  64.         /// <exception cref="ArgumentException">Thrown if number is less than zero.</exception>
  65.         public static string GetPositiveRadix(this int number, int radix)
  66.         {
  67.             if (number < 0)
  68.             {
  69.                 throw new ArgumentException("number is less than zero", nameof(number));
  70.             }
  71.  
  72.             return GetRadix(number, radix);
  73.         }
  74.  
  75.         /// <summary>
  76.         /// Gets the value of a signed integer to its equivalent string representation in a specified radix.
  77.         /// </summary>
  78.         /// <param name="number">Source number.</param>
  79.         /// <param name="radix">Base of the numeral systems.</param>
  80.         /// <returns>The equivalent string representation of the number in a specified radix.</returns>
  81.         /// <exception cref="ArgumentException">Thrown if radix is not equal 8, 10 or 16.</exception>
  82.         public static string GetRadix(this int number, int radix)
  83.         {
  84.             if (radix != 8 && radix != 10 && radix != 16)
  85.             {
  86.                 throw new ArgumentException("radix is not equal 8, 10 or 16", nameof(radix));
  87.             }
  88.  
  89.             string s = string.Empty;
  90.  
  91.             uint uNumber = (uint)number;
  92.  
  93.             while (uNumber > 0)
  94.             {
  95.                 switch (uNumber % radix)
  96.                 {
  97.                     case 0:
  98.                         s = '0' + s;
  99.                         break;
  100.                     case 1:
  101.                         s = '1' + s;
  102.                         break;
  103.                     case 2:
  104.                         s = '2' + s;
  105.                         break;
  106.                     case 3:
  107.                         s = '3' + s;
  108.                         break;
  109.                     case 4:
  110.                         s = '4' + s;
  111.                         break;
  112.                     case 5:
  113.                         s = '5' + s;
  114.                         break;
  115.                     case 6:
  116.                         s = '6' + s;
  117.                         break;
  118.                     case 7:
  119.                         s = '7' + s;
  120.                         break;
  121.                     case 8:
  122.                         s = '8' + s;
  123.                         break;
  124.                     case 9:
  125.                         s = '9' + s;
  126.                         break;
  127.                     case 10:
  128.                         s = 'A' + s;
  129.                         break;
  130.                     case 11:
  131.                         s = 'B' + s;
  132.                         break;
  133.                     case 12:
  134.                         s = 'C' + s;
  135.                         break;
  136.                     case 13:
  137.                         s = 'D' + s;
  138.                         break;
  139.                     case 14:
  140.                         s = 'E' + s;
  141.                         break;
  142.                     case 15:
  143.                         s = 'F' + s;
  144.                         break;
  145.                 }
  146.  
  147.                 uNumber /= (uint)radix;
  148.             }
  149.  
  150.             return s;
  151.         }
  152.     }
  153. }
  154.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement