Advertisement
yahorrr

Untitled

Sep 26th, 2022
850
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.31 KB | None | 0 0
  1. using System;
  2. using System.Globalization;
  3. using System.Reflection.Metadata.Ecma335;
  4.  
  5. namespace NumeralSystems
  6. {
  7.     /// <summary>
  8.     /// Converts a string representations of a numbers to its integer equivalent.
  9.     /// </summary>
  10.     public static class Converter
  11.     {
  12.         /// <summary>
  13.         /// Converts the string representation of a positive number in the octal numeral system to its 32-bit signed integer equivalent.
  14.         /// </summary>
  15.         /// <param name="source">The string representation of a positive number in the octal numeral system.</param>
  16.         /// <returns>A positive decimal value.</returns>
  17.         /// <exception cref="ArgumentException">
  18.         /// Thrown if source string presents a negative number
  19.         /// - or
  20.         /// contains invalid symbols (non-octal alphabetic characters).
  21.         /// Valid octal alphabetic characters: 0,1,2,3,4,5,6,7.
  22.         /// </exception>
  23.         public static int ParsePositiveFromOctal(this string source) => ParsePositiveByRadix(source, 8);
  24.  
  25.         /// <summary>
  26.         /// Converts the string representation of a positive number in the decimal numeral system to its 32-bit signed integer equivalent.
  27.         /// </summary>
  28.         /// <param name="source">The string representation of a positive number in the decimal numeral system.</param>
  29.         /// <returns>A positive decimal value.</returns>
  30.         /// <exception cref="ArgumentException">
  31.         /// Thrown if source string presents a negative number
  32.         /// - or
  33.         /// contains invalid symbols (non-decimal alphabetic characters).
  34.         /// Valid decimal alphabetic characters: 0,1,2,3,4,5,6,7,8,9.
  35.         /// </exception>
  36.         public static int ParsePositiveFromDecimal(this string source) => ParsePositiveByRadix(source, 10);
  37.  
  38.         /// <summary>
  39.         /// Converts the string representation of a positive number in the hex numeral system to its 32-bit signed integer equivalent.
  40.         /// </summary>
  41.         /// <param name="source">The string representation of a positive number in the hex numeral system.</param>
  42.         /// <returns>A positive decimal value.</returns>
  43.         /// <exception cref="ArgumentException">
  44.         /// Thrown if source string presents a negative number
  45.         /// - or
  46.         /// contains invalid symbols (non-hex alphabetic characters).
  47.         /// Valid hex alphabetic characters: 0,1,2,3,4,5,6,7,8,9,A(or a),B(or b),C(or c),D(or d),E(or e),F(or f).
  48.         /// </exception>
  49.         public static int ParsePositiveFromHex(this string source) => ParsePositiveByRadix(source, 16);
  50.  
  51.         /// <summary>
  52.         /// Converts the string representation of a positive number in the octal, decimal or hex numeral system to its 32-bit signed integer equivalent.
  53.         /// </summary>
  54.         /// <param name="source">The string representation of a positive number in the the octal, decimal or hex numeral system.</param>
  55.         /// <param name="radix">The radix.</param>
  56.         /// <returns>A positive decimal value.</returns>
  57.         /// <exception cref="ArgumentException">
  58.         /// Thrown if source string presents a negative number
  59.         /// - or
  60.         /// contains invalid for given numeral system symbols
  61.         /// -or-
  62.         /// the radix is not equal 8, 10 or 16.
  63.         /// </exception>
  64.         public static int ParsePositiveByRadix(this string source, int radix)
  65.         {
  66.             return ParseByRadix(source, radix) < 0 ? throw new ArgumentException("number is must be more than zero", nameof(source)) : ParseByRadix(source, radix);
  67.         }
  68.  
  69.         /// <summary>
  70.         /// Converts the string representation of a signed number in the octal, decimal or hex numeral system to its 32-bit signed integer equivalent.
  71.         /// </summary>
  72.         /// <param name="source">The string representation of a signed number in the the octal, decimal or hex numeral system.</param>
  73.         /// <param name="radix">The radix.</param>
  74.         /// <returns>A signed decimal value.</returns>
  75.         /// <exception cref="ArgumentException">
  76.         /// Thrown if source contains invalid for given numeral system symbols
  77.         /// -or-
  78.         /// the radix is not equal 8, 10 or 16.
  79.         /// </exception>
  80.         public static int ParseByRadix(this string source, int radix)
  81.         {
  82.             if (radix != 8 && radix != 10 && radix != 16)
  83.             {
  84.                 throw new ArgumentException("radix is must be equal 8, 10 or 16", nameof(radix));
  85.             }
  86.  
  87.             int pow = 1;
  88.             int result = 0;
  89.             int buffer = 0;
  90.  
  91.             bool isMinusFirstDigit = source[0] == '-';
  92.  
  93.             for (int i = source.Length - 1; isMinusFirstDigit ? i > 0 : i >= 0; i--)
  94.             {
  95.                 if (!IsValid(source[i], radix))
  96.                 {
  97.                     throw new ArgumentException("source contains invalid for given numeral system symbols", nameof(source));
  98.                 }
  99.  
  100.                 buffer = Getdigit(source[i]);
  101.                 result += buffer * pow;
  102.                 pow *= radix;
  103.             }
  104.  
  105.             return isMinusFirstDigit ? -result : result;
  106.         }
  107.  
  108.         /// <summary>
  109.         /// Converts the string representation of a positive number in the octal numeral system to its 32-bit signed integer equivalent.
  110.         /// A return value indicates whether the conversion succeeded.
  111.         /// </summary>
  112.         /// <param name="source">The string representation of a positive number in the octal numeral system.</param>
  113.         /// <param name="value">A positive decimal value.</param>
  114.         /// <returns>true if s was converted successfully; otherwise, false.</returns>
  115.         public static bool TryParsePositiveFromOctal(this string source, out int value)
  116.         {
  117.             try
  118.             {
  119.                 value = ParsePositiveByRadix(source, 8);
  120.                 return true;
  121.             }
  122.             catch
  123.             {
  124.                 value = default;
  125.                 return false;
  126.             }
  127.         }
  128.  
  129.         /// <summary>
  130.         /// Converts the string representation of a positive number in the decimal numeral system to its 32-bit signed integer equivalent.
  131.         /// A return value indicates whether the conversion succeeded.
  132.         /// </summary>
  133.         /// <param name="source">The string representation of a positive number in the decimal numeral system.</param>
  134.         /// <returns>A positive decimal value.</returns>
  135.         /// <param name="value">A positive decimal value.</param>
  136.         /// <returns>true if s was converted successfully; otherwise, false.</returns>
  137.         public static bool TryParsePositiveFromDecimal(this string source, out int value)
  138.         {
  139.             try
  140.             {
  141.                 value = ParsePositiveByRadix(source, 10);
  142.                 return true;
  143.             }
  144.             catch
  145.             {
  146.                 value = default;
  147.                 return false;
  148.             }
  149.         }
  150.  
  151.         /// <summary>
  152.         /// Converts the string representation of a positive number in the hex numeral system to its 32-bit signed integer equivalent.
  153.         /// A return value indicates whether the conversion succeeded.
  154.         /// </summary>
  155.         /// <param name="source">The string representation of a positive number in the hex numeral system.</param>
  156.         /// <returns>A positive decimal value.</returns>
  157.         /// <param name="value">A positive decimal value.</param>
  158.         /// <returns>true if s was converted successfully; otherwise, false.</returns>
  159.         public static bool TryParsePositiveFromHex(this string source, out int value)
  160.         {
  161.             try
  162.             {
  163.                 value = ParsePositiveByRadix(source, 16);
  164.                 return true;
  165.             }
  166.             catch
  167.             {
  168.                 value = default;
  169.                 return false;
  170.             }
  171.         }
  172.  
  173.         /// <summary>
  174.         /// Converts the string representation of a positive number in the octal, decimal or hex numeral system to its 32-bit signed integer equivalent.
  175.         /// A return value indicates whether the conversion succeeded.
  176.         /// </summary>
  177.         /// <param name="source">The string representation of a positive number in the the octal, decimal or hex numeral system.</param>
  178.         /// <param name="radix">The radix.</param>
  179.         /// <returns>A positive decimal value.</returns>
  180.         /// <param name="value">A positive decimal value.</param>
  181.         /// <returns>true if s was converted successfully; otherwise, false.</returns>
  182.         /// <exception cref="ArgumentException">Thrown the radix is not equal 8, 10 or 16.</exception>
  183.         public static bool TryParsePositiveByRadix(this string source, int radix, out int value)
  184.         {
  185.             if (radix != 8 && radix != 10 && radix != 16)
  186.             {
  187.                 throw new ArgumentException("radix is must be equal 8, 10 or 16", nameof(radix));
  188.             }
  189.  
  190.             try
  191.             {
  192.                 value = ParsePositiveByRadix(source, radix);
  193.                 return true;
  194.             }
  195.             catch
  196.             {
  197.                 value = default;
  198.                 return false;
  199.             }
  200.         }
  201.  
  202.         /// <summary>
  203.         /// Converts the string representation of a signed number in the octal, decimal or hex numeral system to its 32-bit signed integer equivalent.
  204.         /// A return value indicates whether the conversion succeeded.
  205.         /// </summary>
  206.         /// <param name="source">The string representation of a signed number in the the octal, decimal or hex numeral system.</param>
  207.         /// <param name="radix">The radix.</param>
  208.         /// <returns>A positive decimal value.</returns>
  209.         /// <param name="value">A positive decimal value.</param>
  210.         /// <returns>true if s was converted successfully; otherwise, false.</returns>
  211.         /// <exception cref="ArgumentException">Thrown the radix is not equal 8, 10 or 16.</exception>
  212.         public static bool TryParseByRadix(this string source, int radix, out int value)
  213.         {
  214.             if (radix != 8 && radix != 10 && radix != 16)
  215.             {
  216.                 throw new ArgumentException("radix is must be equal 8, 10 or 16", nameof(radix));
  217.             }
  218.  
  219.             try
  220.             {
  221.                 value = ParseByRadix(source, radix);
  222.                 return true;
  223.             }
  224.             catch
  225.             {
  226.                 value = default;
  227.                 return false;
  228.             }
  229.         }
  230.  
  231.         private static int Getdigit(char digit) =>
  232.             char.ToUpper(digit, CultureInfo.InvariantCulture) switch
  233.             {
  234.                 '0' => 0,
  235.                 '1' => 1,
  236.                 '2' => 2,
  237.                 '3' => 3,
  238.                 '4' => 4,
  239.                 '5' => 5,
  240.                 '6' => 6,
  241.                 '7' => 7,
  242.                 '8' => 8,
  243.                 '9' => 9,
  244.                 'A' => 10,
  245.                 'B' => 11,
  246.                 'C' => 12,
  247.                 'D' => 13,
  248.                 'E' => 14,
  249.                 'F' => 15,
  250.                 _ => -1
  251.             };
  252.  
  253.         private static bool IsValid(char digit, int radix)
  254.         {
  255.             if (Getdigit(digit) > radix - 1 || Getdigit(digit) == -1)
  256.             {
  257.                 return false;
  258.             }
  259.  
  260.             return true;
  261.         }
  262.     }
  263. }
  264.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement