Advertisement
yahorrr

Untitled

Sep 26th, 2022
920
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.89 KB | None | 0 0
  1. using System;
  2. using System.Reflection.Metadata.Ecma335;
  3.  
  4. namespace NumeralSystems
  5. {
  6.     /// <summary>
  7.     /// Converts a string representations of a numbers to its integer equivalent.
  8.     /// </summary>
  9.     public static class Converter
  10.     {
  11.         /// <summary>
  12.         /// Converts the string representation of a positive number in the octal numeral system to its 32-bit signed integer equivalent.
  13.         /// </summary>
  14.         /// <param name="source">The string representation of a positive number in the octal numeral system.</param>
  15.         /// <returns>A positive decimal value.</returns>
  16.         /// <exception cref="ArgumentException">
  17.         /// Thrown if source string presents a negative number
  18.         /// - or
  19.         /// contains invalid symbols (non-octal alphabetic characters).
  20.         /// Valid octal alphabetic characters: 0,1,2,3,4,5,6,7.
  21.         /// </exception>
  22.         public static int ParsePositiveFromOctal(this string source) => throw new NotImplementedException();
  23.  
  24.         /// <summary>
  25.         /// Converts the string representation of a positive number in the decimal numeral system to its 32-bit signed integer equivalent.
  26.         /// </summary>
  27.         /// <param name="source">The string representation of a positive number in the decimal numeral system.</param>
  28.         /// <returns>A positive decimal value.</returns>
  29.         /// <exception cref="ArgumentException">
  30.         /// Thrown if source string presents a negative number
  31.         /// - or
  32.         /// contains invalid symbols (non-decimal alphabetic characters).
  33.         /// Valid decimal alphabetic characters: 0,1,2,3,4,5,6,7,8,9.
  34.         /// </exception>
  35.         public static int ParsePositiveFromDecimal(this string source) => throw new NotImplementedException();
  36.  
  37.         /// <summary>
  38.         /// Converts the string representation of a positive number in the hex numeral system to its 32-bit signed integer equivalent.
  39.         /// </summary>
  40.         /// <param name="source">The string representation of a positive number in the hex numeral system.</param>
  41.         /// <returns>A positive decimal value.</returns>
  42.         /// <exception cref="ArgumentException">
  43.         /// Thrown if source string presents a negative number
  44.         /// - or
  45.         /// contains invalid symbols (non-hex alphabetic characters).
  46.         /// 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).
  47.         /// </exception>
  48.         public static int ParsePositiveFromHex(this string source) => throw new NotImplementedException();
  49.  
  50.         /// <summary>
  51.         /// Converts the string representation of a positive number in the octal, decimal or hex numeral system to its 32-bit signed integer equivalent.
  52.         /// </summary>
  53.         /// <param name="source">The string representation of a positive number in the the octal, decimal or hex numeral system.</param>
  54.         /// <param name="radix">The radix.</param>
  55.         /// <returns>A positive decimal value.</returns>
  56.         /// <exception cref="ArgumentException">
  57.         /// Thrown if source string presents a negative number
  58.         /// - or
  59.         /// contains invalid for given numeral system symbols
  60.         /// -or-
  61.         /// the radix is not equal 8, 10 or 16.
  62.         /// </exception>
  63.         public static int ParsePositiveByRadix(this string source, int radix) => throw new NotImplementedException();
  64.  
  65.         /// <summary>
  66.         /// Converts the string representation of a signed number in the octal, decimal or hex numeral system to its 32-bit signed integer equivalent.
  67.         /// </summary>
  68.         /// <param name="source">The string representation of a signed number in the the octal, decimal or hex numeral system.</param>
  69.         /// <param name="radix">The radix.</param>
  70.         /// <returns>A signed decimal value.</returns>
  71.         /// <exception cref="ArgumentException">
  72.         /// Thrown if source contains invalid for given numeral system symbols
  73.         /// -or-
  74.         /// the radix is not equal 8, 10 or 16.
  75.         /// </exception>
  76.         public static int ParseByRadix(this string source, int radix)
  77.         {
  78.             int pow = 1;
  79.             int result = 0;
  80.             int buffer = 0;
  81.  
  82.             for (int i = source.Length - 1; i >= 0; i--)
  83.             {
  84.                 buffer = source[i] switch
  85.                 {
  86.                     '0' => 0,
  87.                     '1' => 1,
  88.                     '2' => 2,
  89.                     '3' => 3,
  90.                     '4' => 4,
  91.                     '5' => 5,
  92.                     '6' => 6,
  93.                     '7' => 7,
  94.                     '8' => 8,
  95.                     '9' => 9,
  96.                     'A' => 10,
  97.                     'B' => 11,
  98.                     'C' => 12,
  99.                     'D' => 13,
  100.                     'E' => 14,
  101.                     'F' => 15,
  102.                     _ => 0
  103.                 };
  104.                 result += buffer * pow;
  105.                 pow *= radix;
  106.             }
  107.  
  108.             return source[0] == '-' ? -result : result;
  109.         }
  110.  
  111.         /// <summary>
  112.         /// Converts the string representation of a positive number in the octal numeral system to its 32-bit signed integer equivalent.
  113.         /// A return value indicates whether the conversion succeeded.
  114.         /// </summary>
  115.         /// <param name="source">The string representation of a positive number in the octal numeral system.</param>
  116.         /// <param name="value">A positive decimal value.</param>
  117.         /// <returns>true if s was converted successfully; otherwise, false.</returns>
  118.         public static bool TryParsePositiveFromOctal(this string source, out int value) => throw new NotImplementedException();
  119.  
  120.         /// <summary>
  121.         /// Converts the string representation of a positive number in the decimal numeral system to its 32-bit signed integer equivalent.
  122.         /// A return value indicates whether the conversion succeeded.
  123.         /// </summary>
  124.         /// <param name="source">The string representation of a positive number in the decimal numeral system.</param>
  125.         /// <returns>A positive decimal value.</returns>
  126.         /// <param name="value">A positive decimal value.</param>
  127.         /// <returns>true if s was converted successfully; otherwise, false.</returns>
  128.         public static bool TryParsePositiveFromDecimal(this string source, out int value) => throw new NotImplementedException();
  129.  
  130.         /// <summary>
  131.         /// Converts the string representation of a positive number in the hex numeral system to its 32-bit signed integer equivalent.
  132.         /// A return value indicates whether the conversion succeeded.
  133.         /// </summary>
  134.         /// <param name="source">The string representation of a positive number in the hex numeral system.</param>
  135.         /// <returns>A positive decimal value.</returns>
  136.         /// <param name="value">A positive decimal value.</param>
  137.         /// <returns>true if s was converted successfully; otherwise, false.</returns>
  138.         public static bool TryParsePositiveFromHex(this string source, out int value) => throw new NotImplementedException();
  139.  
  140.         /// <summary>
  141.         /// Converts the string representation of a positive number in the octal, decimal or hex numeral system to its 32-bit signed integer equivalent.
  142.         /// A return value indicates whether the conversion succeeded.
  143.         /// </summary>
  144.         /// <param name="source">The string representation of a positive number in the the octal, decimal or hex numeral system.</param>
  145.         /// <param name="radix">The radix.</param>
  146.         /// <returns>A positive decimal value.</returns>
  147.         /// <param name="value">A positive decimal value.</param>
  148.         /// <returns>true if s was converted successfully; otherwise, false.</returns>
  149.         /// <exception cref="ArgumentException">Thrown the radix is not equal 8, 10 or 16.</exception>
  150.         public static bool TryParsePositiveByRadix(this string source, int radix, out int value) => throw new NotImplementedException();
  151.  
  152.         /// <summary>
  153.         /// Converts the string representation of a signed number in the octal, decimal or hex numeral system to its 32-bit signed integer equivalent.
  154.         /// A return value indicates whether the conversion succeeded.
  155.         /// </summary>
  156.         /// <param name="source">The string representation of a signed number in the the octal, decimal or hex numeral system.</param>
  157.         /// <param name="radix">The radix.</param>
  158.         /// <returns>A positive decimal value.</returns>
  159.         /// <param name="value">A positive decimal value.</param>
  160.         /// <returns>true if s was converted successfully; otherwise, false.</returns>
  161.         /// <exception cref="ArgumentException">Thrown the radix is not equal 8, 10 or 16.</exception>
  162.         public static bool TryParseByRadix(this string source, int radix, out int value) =>
  163.             throw new NotImplementedException();
  164.     }
  165. }
  166.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement