Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Globalization;
- using System.Reflection.Metadata.Ecma335;
- namespace NumeralSystems
- {
- /// <summary>
- /// Converts a string representations of a numbers to its integer equivalent.
- /// </summary>
- public static class Converter
- {
- /// <summary>
- /// Converts the string representation of a positive number in the octal numeral system to its 32-bit signed integer equivalent.
- /// </summary>
- /// <param name="source">The string representation of a positive number in the octal numeral system.</param>
- /// <returns>A positive decimal value.</returns>
- /// <exception cref="ArgumentException">
- /// Thrown if source string presents a negative number
- /// - or
- /// contains invalid symbols (non-octal alphabetic characters).
- /// Valid octal alphabetic characters: 0,1,2,3,4,5,6,7.
- /// </exception>
- public static int ParsePositiveFromOctal(this string source) => ParsePositiveByRadix(source, 8);
- /// <summary>
- /// Converts the string representation of a positive number in the decimal numeral system to its 32-bit signed integer equivalent.
- /// </summary>
- /// <param name="source">The string representation of a positive number in the decimal numeral system.</param>
- /// <returns>A positive decimal value.</returns>
- /// <exception cref="ArgumentException">
- /// Thrown if source string presents a negative number
- /// - or
- /// contains invalid symbols (non-decimal alphabetic characters).
- /// Valid decimal alphabetic characters: 0,1,2,3,4,5,6,7,8,9.
- /// </exception>
- public static int ParsePositiveFromDecimal(this string source) => ParsePositiveByRadix(source, 10);
- /// <summary>
- /// Converts the string representation of a positive number in the hex numeral system to its 32-bit signed integer equivalent.
- /// </summary>
- /// <param name="source">The string representation of a positive number in the hex numeral system.</param>
- /// <returns>A positive decimal value.</returns>
- /// <exception cref="ArgumentException">
- /// Thrown if source string presents a negative number
- /// - or
- /// contains invalid symbols (non-hex alphabetic characters).
- /// 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).
- /// </exception>
- public static int ParsePositiveFromHex(this string source) => ParsePositiveByRadix(source, 16);
- /// <summary>
- /// Converts the string representation of a positive number in the octal, decimal or hex numeral system to its 32-bit signed integer equivalent.
- /// </summary>
- /// <param name="source">The string representation of a positive number in the the octal, decimal or hex numeral system.</param>
- /// <param name="radix">The radix.</param>
- /// <returns>A positive decimal value.</returns>
- /// <exception cref="ArgumentException">
- /// Thrown if source string presents a negative number
- /// - or
- /// contains invalid for given numeral system symbols
- /// -or-
- /// the radix is not equal 8, 10 or 16.
- /// </exception>
- public static int ParsePositiveByRadix(this string source, int radix)
- {
- return ParseByRadix(source, radix) < 0 ? throw new ArgumentException("number is must be more than zero", nameof(source)) : ParseByRadix(source, radix);
- }
- /// <summary>
- /// Converts the string representation of a signed number in the octal, decimal or hex numeral system to its 32-bit signed integer equivalent.
- /// </summary>
- /// <param name="source">The string representation of a signed number in the the octal, decimal or hex numeral system.</param>
- /// <param name="radix">The radix.</param>
- /// <returns>A signed decimal value.</returns>
- /// <exception cref="ArgumentException">
- /// Thrown if source contains invalid for given numeral system symbols
- /// -or-
- /// the radix is not equal 8, 10 or 16.
- /// </exception>
- public static int ParseByRadix(this string source, int radix)
- {
- if (radix != 8 && radix != 10 && radix != 16)
- {
- throw new ArgumentException("radix is must be equal 8, 10 or 16", nameof(radix));
- }
- int pow = 1;
- int result = 0;
- int buffer = 0;
- bool isMinusFirstDigit = source[0] == '-';
- for (int i = source.Length - 1; isMinusFirstDigit ? i > 0 : i >= 0; i--)
- {
- if (!IsValid(source[i], radix))
- {
- throw new ArgumentException("source contains invalid for given numeral system symbols", nameof(source));
- }
- buffer = Getdigit(source[i]);
- result += buffer * pow;
- pow *= radix;
- }
- return isMinusFirstDigit ? -result : result;
- }
- /// <summary>
- /// Converts the string representation of a positive number in the octal numeral system to its 32-bit signed integer equivalent.
- /// A return value indicates whether the conversion succeeded.
- /// </summary>
- /// <param name="source">The string representation of a positive number in the octal numeral system.</param>
- /// <param name="value">A positive decimal value.</param>
- /// <returns>true if s was converted successfully; otherwise, false.</returns>
- public static bool TryParsePositiveFromOctal(this string source, out int value)
- {
- try
- {
- value = ParsePositiveByRadix(source, 8);
- return true;
- }
- catch
- {
- value = default;
- return false;
- }
- }
- /// <summary>
- /// Converts the string representation of a positive number in the decimal numeral system to its 32-bit signed integer equivalent.
- /// A return value indicates whether the conversion succeeded.
- /// </summary>
- /// <param name="source">The string representation of a positive number in the decimal numeral system.</param>
- /// <returns>A positive decimal value.</returns>
- /// <param name="value">A positive decimal value.</param>
- /// <returns>true if s was converted successfully; otherwise, false.</returns>
- public static bool TryParsePositiveFromDecimal(this string source, out int value)
- {
- try
- {
- value = ParsePositiveByRadix(source, 10);
- return true;
- }
- catch
- {
- value = default;
- return false;
- }
- }
- /// <summary>
- /// Converts the string representation of a positive number in the hex numeral system to its 32-bit signed integer equivalent.
- /// A return value indicates whether the conversion succeeded.
- /// </summary>
- /// <param name="source">The string representation of a positive number in the hex numeral system.</param>
- /// <returns>A positive decimal value.</returns>
- /// <param name="value">A positive decimal value.</param>
- /// <returns>true if s was converted successfully; otherwise, false.</returns>
- public static bool TryParsePositiveFromHex(this string source, out int value)
- {
- try
- {
- value = ParsePositiveByRadix(source, 16);
- return true;
- }
- catch
- {
- value = default;
- return false;
- }
- }
- /// <summary>
- /// Converts the string representation of a positive number in the octal, decimal or hex numeral system to its 32-bit signed integer equivalent.
- /// A return value indicates whether the conversion succeeded.
- /// </summary>
- /// <param name="source">The string representation of a positive number in the the octal, decimal or hex numeral system.</param>
- /// <param name="radix">The radix.</param>
- /// <returns>A positive decimal value.</returns>
- /// <param name="value">A positive decimal value.</param>
- /// <returns>true if s was converted successfully; otherwise, false.</returns>
- /// <exception cref="ArgumentException">Thrown the radix is not equal 8, 10 or 16.</exception>
- public static bool TryParsePositiveByRadix(this string source, int radix, out int value)
- {
- if (radix != 8 && radix != 10 && radix != 16)
- {
- throw new ArgumentException("radix is must be equal 8, 10 or 16", nameof(radix));
- }
- try
- {
- value = ParsePositiveByRadix(source, radix);
- return true;
- }
- catch
- {
- value = default;
- return false;
- }
- }
- /// <summary>
- /// Converts the string representation of a signed number in the octal, decimal or hex numeral system to its 32-bit signed integer equivalent.
- /// A return value indicates whether the conversion succeeded.
- /// </summary>
- /// <param name="source">The string representation of a signed number in the the octal, decimal or hex numeral system.</param>
- /// <param name="radix">The radix.</param>
- /// <returns>A positive decimal value.</returns>
- /// <param name="value">A positive decimal value.</param>
- /// <returns>true if s was converted successfully; otherwise, false.</returns>
- /// <exception cref="ArgumentException">Thrown the radix is not equal 8, 10 or 16.</exception>
- public static bool TryParseByRadix(this string source, int radix, out int value)
- {
- if (radix != 8 && radix != 10 && radix != 16)
- {
- throw new ArgumentException("radix is must be equal 8, 10 or 16", nameof(radix));
- }
- try
- {
- value = ParseByRadix(source, radix);
- return true;
- }
- catch
- {
- value = default;
- return false;
- }
- }
- private static int Getdigit(char digit) =>
- char.ToUpper(digit, CultureInfo.InvariantCulture) switch
- {
- '0' => 0,
- '1' => 1,
- '2' => 2,
- '3' => 3,
- '4' => 4,
- '5' => 5,
- '6' => 6,
- '7' => 7,
- '8' => 8,
- '9' => 9,
- 'A' => 10,
- 'B' => 11,
- 'C' => 12,
- 'D' => 13,
- 'E' => 14,
- 'F' => 15,
- _ => -1
- };
- private static bool IsValid(char digit, int radix)
- {
- if (Getdigit(digit) > radix - 1 || Getdigit(digit) == -1)
- {
- return false;
- }
- return true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement