Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO.Pipes;
- using System.Threading;
- namespace NumeralSystems
- {
- public static class Converter
- {
- /// <summary>
- /// Gets the value of a positive integer to its equivalent string representation in the octal numeral systems.
- /// </summary>
- /// <param name="number">Source number.</param>
- /// <returns>The equivalent string representation of the number in the octal numeral systems.</returns>
- /// <exception cref="ArgumentException">Thrown if number is less than zero.</exception>
- public static string GetPositiveOctal(this int number)
- {
- if (number < 0)
- {
- throw new ArgumentException("number is less than zero", nameof(number));
- }
- return GetRadix(number, 8);
- }
- /// <summary>
- /// Gets the value of a positive integer to its equivalent string representation in the decimal numeral systems.
- /// </summary>
- /// <param name="number">Source number.</param>
- /// <returns>The equivalent string representation of the number in the decimal numeral systems.</returns>
- /// <exception cref="ArgumentException">Thrown if number is less than zero.</exception>
- public static string GetPositiveDecimal(this int number)
- {
- if (number < 0)
- {
- throw new ArgumentException("number is less than zero", nameof(number));
- }
- return GetRadix(number, 10);
- }
- /// <summary>
- /// Gets the value of a positive integer to its equivalent string representation in the hexadecimal numeral systems.
- /// </summary>
- /// <param name="number">Source number.</param>
- /// <returns>The equivalent string representation of the number in the hexadecimal numeral systems.</returns>
- /// <exception cref="ArgumentException">Thrown if number is less than zero.</exception>
- public static string GetPositiveHex(this int number)
- {
- if (number < 0)
- {
- throw new ArgumentException("number is less than zero", nameof(number));
- }
- return GetRadix(number, 16);
- }
- /// <summary>
- /// Gets the value of a positive integer to its equivalent string representation in a specified radix.
- /// </summary>
- /// <param name="number">Source number.</param>
- /// <param name="radix">Base of the numeral systems.</param>
- /// <returns>The equivalent string representation of the number in a specified radix.</returns>
- /// <exception cref="ArgumentException">Thrown if radix is not equal 8, 10 or 16.</exception>
- /// <exception cref="ArgumentException">Thrown if number is less than zero.</exception>
- public static string GetPositiveRadix(this int number, int radix)
- {
- if (number < 0)
- {
- throw new ArgumentException("number is less than zero", nameof(number));
- }
- return GetRadix(number, radix);
- }
- /// <summary>
- /// Gets the value of a signed integer to its equivalent string representation in a specified radix.
- /// </summary>
- /// <param name="number">Source number.</param>
- /// <param name="radix">Base of the numeral systems.</param>
- /// <returns>The equivalent string representation of the number in a specified radix.</returns>
- /// <exception cref="ArgumentException">Thrown if radix is not equal 8, 10 or 16.</exception>
- public static string GetRadix(this int number, int radix)
- {
- if (radix != 8 && radix != 10 && radix != 16)
- {
- throw new ArgumentException("radix is not equal 8, 10 or 16", nameof(radix));
- }
- string s = string.Empty;
- uint uNumber = (uint)number;
- while (uNumber > 0)
- {
- switch (uNumber % radix)
- {
- case 0:
- s = '0' + s;
- break;
- case 1:
- s = '1' + s;
- break;
- case 2:
- s = '2' + s;
- break;
- case 3:
- s = '3' + s;
- break;
- case 4:
- s = '4' + s;
- break;
- case 5:
- s = '5' + s;
- break;
- case 6:
- s = '6' + s;
- break;
- case 7:
- s = '7' + s;
- break;
- case 8:
- s = '8' + s;
- break;
- case 9:
- s = '9' + s;
- break;
- case 10:
- s = 'A' + s;
- break;
- case 11:
- s = 'B' + s;
- break;
- case 12:
- s = 'C' + s;
- break;
- case 13:
- s = 'D' + s;
- break;
- case 14:
- s = 'E' + s;
- break;
- case 15:
- s = 'F' + s;
- break;
- }
- uNumber /= (uint)radix;
- }
- return s;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement