示例#1
0
 private BaseConverter(int fromRadix, NumberingSchemes fromScheme, int toRadix, NumberingSchemes toScheme)
 {
     #region Validate arguments
     if (fromRadix < 2 || fromRadix > 36)
     {
         throw new ArgumentOutOfRangeException("fromRadix", "Radix can be from 2 to 36 inclusive");
     }
     if (toRadix < 2 || toRadix > 36)
     {
         throw new ArgumentOutOfRangeException("toRadix", "Radix can be from 2 to 36 inclusive");
     }
     if (fromRadix > 26 && fromScheme == NumberingSchemes.AToZ)
     {
         throw new ArgumentOutOfRangeException("fromRadix", "Invalid numbering scheme for specified number base");
     }
     if (toRadix > 26 && fromScheme == NumberingSchemes.AToZ)
     {
         throw new ArgumentOutOfRangeException("toRadix", "Invalid numbering scheme for specified number base");
     }
     #endregion
     _fromRadix              = fromRadix;
     _fromNumberingScheme    = GetCharactersForNumberingScheme(fromScheme);
     _toRadix                = toRadix;
     _toNumberingScheme      = GetCharactersForNumberingScheme(toScheme);
     _maxFromSchemeCharacter = (fromScheme == NumberingSchemes.ZeroToZ) ? fromRadix : fromRadix + 1;
 }
示例#2
0
        /// <summary>
        /// Convert a number from the specified number base to the specified number base using the
        /// specified numbering schemes
        /// </summary>
        /// <param name="fromRadix">Number base to convert from (1 to 36)</param>
        /// <param name="fromScheme">Numbering scheme to convert from</param>
        /// <param name="toRadix">Number base to convert to (1 to 36)</param>
        /// <param name="toScheme">Numbering scheme to convert to</param>
        /// <param name="value">Value to convert</param>
        /// <returns>Converted value</returns>
        /// <exception cref="ArgumentNullException">Thrown for null arguments</exception>
        /// <remarks>This method should for ad-hoc conversions. Use the
        /// factory methods to create an instance for multiple conversions</remarks>
        public static string Convert(int fromRadix, NumberingSchemes fromScheme, int toRadix, NumberingSchemes toScheme, string value)
        {
            // note, arguments are validated elsewhere

            Converter converter = new Converter(fromRadix, fromScheme, toRadix, toScheme);

            return(converter.Convert(value));
        }
示例#3
0
        private static string GetCharactersForNumberingScheme(NumberingSchemes scheme)
        {
            string characters;

            switch (scheme)
            {
            case NumberingSchemes.AToZ:
                characters = "_ABCDEFGHIJKLMNOPQRSTUVWXYZ";
                break;

            case NumberingSchemes.ZeroToZ:
                characters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
                break;

            default:
                throw new ArgumentOutOfRangeException("scheme");
            }
            return(characters);
        }
示例#4
0
        public static string Convert(int fromRadix, NumberingSchemes fromScheme, int toRadix, NumberingSchemes toScheme, string value)
        {
            BaseConverter BaseConverter = new BaseConverter(fromRadix, fromScheme, toRadix, toScheme);

            return(BaseConverter.Convert(value));
        }
示例#5
0
        /// <summary>
        /// Returns characters to be used in the specified numbering scheme
        /// </summary>
        /// <param name="scheme">Numbering scheme to return</param>
        /// <returns>String of characters</returns>
        private static string GetCharactersForNumberingScheme(NumberingSchemes scheme)
        {
            string characters;

            switch (scheme)
            {
                case NumberingSchemes.AToZ:
                    characters = "_ABCDEFGHIJKLMNOPQRSTUVWXYZ";
                    break;

                case NumberingSchemes.ZeroToZ:
                    characters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
                    break;

                default:
                    throw new ArgumentOutOfRangeException("scheme");
            }

            return characters;
        }
示例#6
0
 /// <summary>
 /// Factory method.
 /// Returns a new <see cref="Converter"/> to convert between two different
 /// number bases using the specified numbering schemes
 /// </summary>
 /// <param name="fromRadix">Number base to convert from</param>
 /// <param name="fromScheme">Numbering scheme to convert from</param>
 /// <param name="toRadix">Number base to convert to (1 to 36)</param>
 /// <param name="toScheme">Numbering scheme to convert to</param>
 /// <returns>New instance</returns>
 /// <exception cref="ArgumentOutOfRangeException">
 /// Thrown if unsupported number bases are specified for <paramref name="fromRadix"/> or
 /// <paramref name="toRadix"/>
 /// </exception>
 /// <example>
 /// Shows how to create a <see cref="Converter"/> instance to convert from
 /// Base 8 to Hexadecimal using numbering schemes 0-9 and 0-Z respectively
 /// <code>
 /// Converter o = Converter.Create(NumberBases.Octal, NumberingSchemes.ZeroToZ, 
 ///     16, NumberingSchemes.ZeroToZ);
 /// string value = o.Convert("4321");
 /// </code>
 /// </example>
 /// <remarks>This method should be used to create a <see cref="Converter"/> that is
 /// to be used many times by client code. For ad-hoc conversions, use the static class
 /// conversion methods</remarks>
 public static Converter Create(NumberBases fromRadix, NumberingSchemes fromScheme, int toRadix, NumberingSchemes toScheme)
 {
     return new Converter((int)fromRadix, fromScheme, toRadix, toScheme);
 }
示例#7
0
        /// <summary>
        /// Convert a number from the specified number base to the specified number base using the 
        /// specified numbering schemes
        /// </summary>
        /// <param name="fromRadix">Number base to convert from (1 to 36)</param>
        /// <param name="fromScheme">Numbering scheme to convert from</param>
        /// <param name="toRadix">Number base to convert to (1 to 36)</param>
        /// <param name="toScheme">Numbering scheme to convert to</param>
        /// <param name="value">Value to convert</param>
        /// <returns>Converted value</returns>
        /// <exception cref="ArgumentNullException">Thrown for null arguments</exception>
        /// <remarks>This method should for ad-hoc conversions. Use the 
        /// factory methods to create an instance for multiple conversions</remarks>
        public static string Convert(int fromRadix, NumberingSchemes fromScheme, int toRadix, NumberingSchemes toScheme, string value)
        {
            // note, arguments are validated elsewhere

            Converter converter = new Converter(fromRadix, fromScheme, toRadix, toScheme);

            return converter.Convert(value);
        }
示例#8
0
 /// <summary>
 /// Convert a number from the specified number base to the specified number base using the 
 /// specified numbering schemes
 /// </summary>
 /// <param name="fromRadix">Number base to convert from</param>
 /// <param name="fromScheme">Numbering scheme to convert from</param>
 /// <param name="toRadix">Number base to convert to (1 to 36)</param>
 /// <param name="toScheme">Numbering scheme to convert to</param>
 /// <param name="value">Value to convert</param>
 /// <returns>Converted value</returns>
 /// <exception cref="ArgumentNullException">Thrown for null arguments</exception>
 /// <remarks>This method should for ad-hoc conversions. Use the 
 /// factory methods to create an instance for multiple conversions</remarks>
 public static string Convert(int fromRadix, NumberingSchemes fromScheme, NumberBases toRadix, NumberingSchemes toScheme, string value)
 {
     return Convert(fromRadix, fromScheme, (int)toRadix, toScheme, value);
 }
示例#9
0
        /// <summary>
        /// Creates a new instance
        /// </summary>
        /// <param name="fromRadix">Number base to convert from</param>
        /// <param name="fromScheme">Numbering scheme to convert from</param>
        /// <param name="toRadix">Number base to convert to</param>
        /// <param name="toScheme">Numbering scheme to convert to</param>
        /// <exception cref="ArgumentOutOfRangeException">
        /// Thrown if unsupported number bases are specified for <paramref name="fromRadix"/> or
        /// <paramref name="toRadix"/>
        /// </exception>
        private Converter(int fromRadix, NumberingSchemes fromScheme, int toRadix, NumberingSchemes toScheme)
        {
            #region Validate arguments
            if (fromRadix < 2 || fromRadix > 36)
                throw new ArgumentOutOfRangeException("fromRadix", "Radix can be from 2 to 36 inclusive");
            if (toRadix < 2 || toRadix > 36)
                throw new ArgumentOutOfRangeException("toRadix", "Radix can be from 2 to 36 inclusive");
            if (fromRadix > 26 && fromScheme == NumberingSchemes.AToZ)
                throw new ArgumentOutOfRangeException("fromRadix", "Invalid numbering scheme for specified number base");
            if (toRadix > 26 && fromScheme == NumberingSchemes.AToZ)
                throw new ArgumentOutOfRangeException("toRadix", "Invalid numbering scheme for specified number base");

            #endregion

            _fromRadix = fromRadix;
            _fromNumberingScheme = GetCharactersForNumberingScheme(fromScheme);

            _toRadix = toRadix;
            _toNumberingScheme = GetCharactersForNumberingScheme(toScheme);

            // determine max character allowed in numbering scheme
            _maxFromSchemeCharacter = (fromScheme == NumberingSchemes.ZeroToZ) ? fromRadix : fromRadix + 1;
        }
示例#10
0
 /// <summary>
 /// Convert a number from the specified number base to the specified number base using the
 /// specified numbering schemes
 /// </summary>
 /// <param name="fromRadix">Number base to convert from</param>
 /// <param name="fromScheme">Numbering scheme to convert from</param>
 /// <param name="toRadix">Number base to convert to (1 to 36)</param>
 /// <param name="toScheme">Numbering scheme to convert to</param>
 /// <param name="value">Value to convert</param>
 /// <returns>Converted value</returns>
 /// <exception cref="ArgumentNullException">Thrown for null arguments</exception>
 /// <remarks>This method should for ad-hoc conversions. Use the
 /// factory methods to create an instance for multiple conversions</remarks>
 public static string Convert(int fromRadix, NumberingSchemes fromScheme, NumberBases toRadix, NumberingSchemes toScheme, string value)
 {
     return(Convert(fromRadix, fromScheme, (int)toRadix, toScheme, value));
 }
示例#11
0
 /// <summary>
 /// Factory method.
 /// Returns a new <see cref="Converter"/> to convert between two different
 /// number bases using the specified numbering schemes
 /// </summary>
 /// <param name="fromRadix">Number base to convert from</param>
 /// <param name="fromScheme">Numbering scheme to convert from</param>
 /// <param name="toRadix">Number base to convert to (1 to 36)</param>
 /// <param name="toScheme">Numbering scheme to convert to</param>
 /// <returns>New instance</returns>
 /// <exception cref="ArgumentOutOfRangeException">
 /// Thrown if unsupported number bases are specified for <paramref name="fromRadix"/> or
 /// <paramref name="toRadix"/>
 /// </exception>
 /// <example>
 /// Shows how to create a <see cref="Converter"/> instance to convert from
 /// Base 8 to Hexadecimal using numbering schemes 0-9 and 0-Z respectively
 /// <code>
 /// Converter o = Converter.Create(NumberBases.Octal, NumberingSchemes.ZeroToZ,
 ///     16, NumberingSchemes.ZeroToZ);
 /// string value = o.Convert("4321");
 /// </code>
 /// </example>
 /// <remarks>This method should be used to create a <see cref="Converter"/> that is
 /// to be used many times by client code. For ad-hoc conversions, use the static class
 /// conversion methods</remarks>
 public static Converter Create(NumberBases fromRadix, NumberingSchemes fromScheme, int toRadix, NumberingSchemes toScheme)
 {
     return(new Converter((int)fromRadix, fromScheme, toRadix, toScheme));
 }