diff --git a/SweetLib/Utils/Extensions/BaseTypeExtensions.cs b/SweetLib/Utils/Extensions/BaseTypeExtensions.cs new file mode 100644 index 0000000..1131ad2 --- /dev/null +++ b/SweetLib/Utils/Extensions/BaseTypeExtensions.cs @@ -0,0 +1,214 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace SweetLib.Utils.Extensions +{ + /// + /// Defines extensions to some general classes. + /// + public static class BaseTypeExtensions + { + /// + /// Converts a value into a byte array. + /// + /// Value to convert. + /// Byte array representation of the value. + public static byte[] ToBytes(this short value) + { + return BitConverter.GetBytes(value); + } + + /// + /// Converts a value into a byte array. + /// + /// Value to convert. + /// Byte array representation of the value. + public static byte[] ToBytes(this ushort value) + { + return BitConverter.GetBytes(value); + } + + /// + /// Converts a value into a byte array. + /// + /// Value to convert. + /// Byte array representation of the value. + public static byte[] ToBytes(this int value) + { + return BitConverter.GetBytes(value); + } + + /// + /// Converts a value into a byte array. + /// + /// Value to convert. + /// Byte array representation of the value. + public static byte[] ToBytes(this uint value) + { + return BitConverter.GetBytes(value); + } + + /// + /// Converts a value into a byte array. + /// + /// Value to convert. + /// Byte array representation of the value. + public static byte[] ToBytes(this long value) + { + return BitConverter.GetBytes(value); + } + + /// + /// Converts a value into a byte array. + /// + /// Value to convert. + /// Byte array representation of the value. + public static byte[] ToBytes(this ulong value) + { + return BitConverter.GetBytes(value); + } + + /// + /// Converts a value into a byte array. + /// + /// Value to convert. + /// Byte array representation of the value. + public static byte[] ToBytes(this float value) + { + return BitConverter.GetBytes(value); + } + + /// + /// Converts a value into a byte array. + /// + /// Value to convert. + /// Byte array representation of the value. + public static byte[] ToBytes(this double value) + { + return BitConverter.GetBytes(value); + } + + /// + /// Converts a value into a byte array. + /// + /// Value to convert. + /// Byte array representation of the value. + public static byte[] ToBytes(this bool value) + { + return BitConverter.GetBytes(value); + } + + /// + /// Converts a value into a byte array. + /// + /// Value to convert. + /// Byte array representation of the value. + public static byte[] ToBytes(this DateTime value) + { + return BitConverter.GetBytes(value.ToUnixTimeStamp()); + } + + /// + /// Converts a value into a byte array. + /// + /// Value to convert. + /// Byte array representation of the value. + /// Using for converting. + public static byte[] ToBytes(this string value) + { + return Encoding.UTF32.GetBytes(value); + } + + /// + /// Converts a into a type. + /// + /// Byte array representation to convert. + /// Converted value. + public static short ToInt16(this byte[] bytes) + { + return BitConverter.ToInt16(bytes, 0); + } + + /// + /// Converts a into a type. + /// + /// Byte array representation to convert. + /// Converted value. + public static ushort ToUInt16(this byte[] bytes) + { + return BitConverter.ToUInt16(bytes, 0); + } + + /// + /// Converts a into a type. + /// + /// Byte array representation to convert. + /// Converted value. + public static int ToInt32(this byte[] bytes) + { + return BitConverter.ToInt32(bytes, 0); + } + + /// + /// Converts a into a type. + /// + /// Byte array representation to convert. + /// Converted value. + public static uint ToUInt32(this byte[] bytes) + { + return BitConverter.ToUInt32(bytes, 0); + } + + /// + /// Converts a into a type. + /// + /// Byte array representation to convert. + /// Converted value. + public static long ToInt64(this byte[] bytes) + { + return BitConverter.ToInt64(bytes, 0); + } + + /// + /// Converts a into a type. + /// + /// Byte array representation to convert. + /// Converted value. + public static ulong ToUInt64(this byte[] bytes) + { + return BitConverter.ToUInt64(bytes, 0); + } + + /// + /// Converts a into a type. + /// + /// Byte array representation to convert. + /// Converted value. + public static float ToSingle(this byte[] bytes) + { + return BitConverter.ToSingle(bytes, 0); + } + + /// + /// Converts a into a type. + /// + /// Byte array representation to convert. + /// Converted value. + public static double ToDouble(this byte[] bytes) + { + return BitConverter.ToDouble(bytes, 0); + } + + /// + /// Converts a into a type. + /// + /// Byte array representation to convert. + /// Converted value. + /// Using for converting. + public static string ToString(this byte[] bytes) + { + return Encoding.UTF32.GetString(bytes); + } + } +} diff --git a/SweetLib/Utils/Extensions/DateTimeExtensions.cs b/SweetLib/Utils/Extensions/DateTimeExtensions.cs new file mode 100644 index 0000000..0ed3211 --- /dev/null +++ b/SweetLib/Utils/Extensions/DateTimeExtensions.cs @@ -0,0 +1,32 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace SweetLib.Utils.Extensions +{ + /// + /// Defines extensions to classes which are related to . + /// + public static class DateTimeExtensions + { + /// + /// Converts a into an Unix timestamp. + /// + /// to convert into Unix timestamp. + /// Converted Unix timestamp. + public static double ToUnixTimeStamp(this DateTime date) + { + return date.Subtract(new DateTime(1970, 1, 1)).TotalSeconds; + } + + /// + /// Converts an Unix timestamp into a . + /// + /// Unix timestamp to convert. + /// Converted . + public static DateTime ToDateTime(this double timestamp) + { + return new DateTime(1970, 1, 1).AddMilliseconds(timestamp); + } + } +} diff --git a/SweetLib/Utils/SweetUtils.cs b/SweetLib/Utils/SweetUtils.cs index c32fc4b..0cfd330 100644 --- a/SweetLib/Utils/SweetUtils.cs +++ b/SweetLib/Utils/SweetUtils.cs @@ -36,25 +36,5 @@ namespace SweetLib.Utils return invalidChars.Aggregate(fileName, (current, c) => current.Replace(c, replaceChar)); } - - /// - /// Converts a into an Unix timestamp. - /// - /// to convert into Unix timestamp. - /// Converted Unix timestamp. - public static double DateTimeToUnixTimeStamp(DateTime date) - { - return date.Subtract(new DateTime(1970, 1, 1)).TotalSeconds; - } - - /// - /// Converts an Unix timestamp into a . - /// - /// Unix timestamp to convert. - /// Converted . - public static DateTime UnixTimestampToDateTime(double timestamp) - { - return new DateTime(1970, 1, 1).AddMilliseconds(timestamp); - } } }