using System; using System.Globalization; namespace Ubisoft { /// /// Utility class for dealing with int type. /// public static class HtInt { public static readonly IFormatProvider DefaultFormatProvider = HtTypes.DefaultFormatProvider; /// /// Converts the string representation of a number to its 32-bit signed integer equivalent. A return value indicates whether the conversion succeeded. /// /// This method uses NumberStyles.Integer and CultureInfo.InvariantCulture. /// Use this method when the string to convert comes from Content or persistence. /// /// A string containing a number to convert. /// When this method returns, contains the 32-bit signed integer value equivalent of the number contained in s, /// if the conversion succeeded, or zero if the conversion failed. The conversion fails if the s parameter is null or Empty, /// is not of the correct format, or represents a number less than MinValue or greater than MaxValue. /// This parameter is passed uninitialized; any value originally supplied in result will be overwritten. /// true if s was converted successfully; otherwise, false public static bool TryParse(string s, out int result) { return TryParse(s, DefaultFormatProvider, out result); } /// /// Converts the string representation of a number to its 32-bit signed integer equivalent. A return value indicates whether the conversion succeeded. /// /// This method uses NumberStyles.Integer. /// /// A string containing a number to convert. /// An object that supplies culture-specific formatting information about s. /// When this method returns, contains the 32-bit signed integer value equivalent of the number contained in s, /// if the conversion succeeded, or zero if the conversion failed. The conversion fails if the s parameter is null or Empty, /// is not of the correct format, or represents a number less than MinValue or greater than MaxValue. /// This parameter is passed uninitialized; any value originally supplied in result will be overwritten. /// true if s was converted successfully; otherwise, false public static bool TryParse(string s, IFormatProvider formatProvider, out int result) { return int.TryParse(s, NumberStyles.Integer, formatProvider, out result); } /// /// Converts the string representation of a number to its 32-bit signed integer equivalent. /// /// This method uses NumberStyles.Integer and CultureInfo.InvariantCulture. /// Use this method when the string to convert comes from Content or persistence, otherwise use int.Parse() instead. /// /// A string containing a number to convert. /// A 32-bit signed integer equivalent to the number specified in s /// or /// ArgumentNullException if s is null. /// or /// FormatException if s is not in a format compliant with CultureInfo.InvariantCulture. /// or /// OverflowException if s represents a number less than MinValue or greater than MaxValue or s includes non-zero, fractional digits. /// public static int Parse(string s) { return Parse(s, DefaultFormatProvider); } /// /// Converts the string representation of a number to its 32-bit signed integer equivalent. /// /// This method uses NumberStyles.Integer. /// Use this method when the string to convert comes from Content or persistence. /// /// A string containing a number to convert. /// An object that supplies culture-specific formatting information about s. /// A 32-bit signed integer equivalent to the number specified in s /// or /// ArgumentNullException if s is null. /// or /// FormatException if s is not in a format compliant with CultureInfo.InvariantCulture. /// or /// OverflowException if s represents a number less than MinValue or greater than MaxValue or s includes non-zero, fractional digits. /// public static int Parse(string s, IFormatProvider formatProvider) { return int.Parse(s, NumberStyles.Integer, formatProvider); } /// /// Converts the numeric value passed in to its equivalent string representation using CultureInfo.InvariantCulture. /// /// Use this method when you need the string returned to be independent from user's culture, typically when storing persistence, which /// will be loaded by using HtInt.Parse() or HtInt.TryParse() methods. /// /// A 32-bit signed integer. /// The string representation of the value. public static string ToString(int value) { return value.ToString(DefaultFormatProvider); } #region extension /// /// Copies an int to a byte array: Byte order and sift order are inverted. /// /// The integer to convert. /// An arbitrary array of bytes. /// Offset into the desination array. public static void CopyToByteArray(this int source, byte[] destination, int offset) { if (destination == null) { throw new ArgumentException("Destination array cannot be null"); } // check if there is enough space for all the 4 bytes we will copy if (destination.Length < offset + sizeof(int)) { throw new ArgumentException("Not enough room in the destination array"); } for (int i = 0, j = sizeof(int) - 1; i < sizeof(int); i++, --j) { destination[offset + i] = (byte)(source >> (8 * j)); } } /// /// Copies an int to a to byte array Little Endian: Byte order and sift order are the same. /// /// The integer to convert. /// An arbitrary array of bytes. /// Offset into the desination array. public static void CopyToByteArrayLE(this int source, byte[] destination, int offset) { if (destination == null) { throw new ArgumentException("Destination array cannot be null"); } // check if there is enough space for all the 4 bytes we will copy if (destination.Length < offset + sizeof(int)) { throw new ArgumentException("Not enough room in the destination array"); } for (int i = 0, j = 0; i < sizeof(int); i++, j++) { destination[offset + i] = (byte)(source >> (8 * j)); } } #endregion } }