using System;
using System.Globalization;
namespace Ubisoft
{
///
/// Utility class for dealing with byte data.
///
public static class HtByte
{
public static readonly IFormatProvider DefaultFormatProvider = HtTypes.DefaultFormatProvider;
///
/// Tries to convert the string representation of a number to its Byte equivalent, and returns a value that 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 Byte value equivalent to the number contained in s if the conversion succeeded,
/// or zero if the conversion failed.
/// 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 byte result)
{
return TryParse(s, DefaultFormatProvider, out result);
}
///
/// Converts the string representation of a number to Byte 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 Byte 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.
/// 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 byte result)
{
return byte.TryParse(s, NumberStyles.Integer, formatProvider, out result);
}
///
/// Converts the string representation of a number to its Byte equivalent.
///
/// 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.
/// A Byte 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 byte Parse(string s)
{
return Parse(s, DefaultFormatProvider);
}
///
/// Converts the string representation of a number to its Byte 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 Byte 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 byte Parse(string s, IFormatProvider formatProvider)
{
return byte.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 HtByte.Parse() or HtByte.TryParse() methods.
///
/// A Byte.
/// The string representation of the value.
public static string ToString(byte value)
{
return value.ToString(DefaultFormatProvider);
}
private static uint[] s_crc32Table = null;
///
/// Calculate CRC32 checksum of a sequence of bytes that can be used for validating data integrity.
///
/// Sequence of bytes to calculate CRC32 for.
/// A long containing the CRC32 checksum of bytes.
public static long Crc32(byte[] bytes)
{
if (s_crc32Table == null)
{
s_crc32Table = new uint[256];
uint poly = 0xedb88320;
for (uint i = 0; i < s_crc32Table.Length; ++i)
{
uint temp = i;
for (int j = 8; j > 0; --j)
{
if ((temp & 1) == 1)
{
temp = (temp >> 1) ^ poly;
}
else
{
temp >>= 1;
}
}
s_crc32Table[i] = temp;
}
}
uint crc = 0xffffffff;
for (int i = 0; i < bytes.Length; ++i)
{
byte index = (byte)(((crc) & 0xff) ^ bytes[i]);
crc = (crc >> 8) ^ s_crc32Table[index];
}
return ~crc;
}
}
}