Refactored method names and added missing conversions from byte arrays

This commit is contained in:
Serraniel 2018-04-06 17:31:36 +02:00
parent 066f320a36
commit 84e67b1e5c
3 changed files with 86 additions and 25 deletions

View file

@ -1,5 +1,6 @@
using System.IO;
using SweetLib.Classes.Storer;
using SweetLib.IO.Classes.Streaming;
using SweetLib.Utils.Logger;
namespace SweetLib.Demo.Console
@ -17,11 +18,24 @@ namespace SweetLib.Demo.Console
Logger.Warn("WARN!");
Logger.Error("Error :(");
System.Console.ReadLine();
var f = Path.GetTempFileName();
var ini = new IniFileStorer(f);
System.Console.WriteLine(ini.ReadString("sec","key"));
using (var stream = File.Create(@"S:\test.dat"))
{
StreamManager.SaveToStream(stream, 5);
}
using (var stream = File.OpenRead(@"S:\test.dat"))
{
int value;
StreamManager.LoadFromStream(stream, out value);
System.Console.WriteLine(value);
}
System.Console.ReadLine();
}
}
}

View file

@ -40,9 +40,6 @@
<Reference Include="Microsoft.Win32.Registry, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Win32.Registry.4.3.0\lib\net46\Microsoft.Win32.Registry.dll</HintPath>
</Reference>
<Reference Include="SweetLib, Version=0.2.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\SweetLib.0.2.0-alpha\lib\netstandard1.3\SweetLib.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.AppContext, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.AppContext.4.3.0\lib\net46\System.AppContext.dll</HintPath>
@ -115,6 +112,16 @@
<None Include="App.config" />
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\SweetLib.IO\SweetLib.IO.csproj">
<Project>{0874043e-42b3-43b2-b3bb-726cee4142e9}</Project>
<Name>SweetLib.IO</Name>
</ProjectReference>
<ProjectReference Include="..\SweetLib\SweetLib.csproj">
<Project>{7c15f2f3-2bf2-46fb-b813-9eec1b41d679}</Project>
<Name>SweetLib</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.

View file

@ -13,7 +13,7 @@ namespace SweetLib.Utils.Extensions
/// </summary>
/// <param name="value">Value to convert.</param>
/// <returns>Byte array representation of the value.</returns>
public static byte[] ToBytes(this short value)
public static byte[] AsBytes(this short value)
{
return BitConverter.GetBytes(value);
}
@ -23,7 +23,7 @@ namespace SweetLib.Utils.Extensions
/// </summary>
/// <param name="value">Value to convert.</param>
/// <returns>Byte array representation of the value.</returns>
public static byte[] ToBytes(this ushort value)
public static byte[] AsBytes(this ushort value)
{
return BitConverter.GetBytes(value);
}
@ -33,7 +33,7 @@ namespace SweetLib.Utils.Extensions
/// </summary>
/// <param name="value">Value to convert.</param>
/// <returns>Byte array representation of the value.</returns>
public static byte[] ToBytes(this int value)
public static byte[] AsBytes(this int value)
{
return BitConverter.GetBytes(value);
}
@ -43,7 +43,7 @@ namespace SweetLib.Utils.Extensions
/// </summary>
/// <param name="value">Value to convert.</param>
/// <returns>Byte array representation of the value.</returns>
public static byte[] ToBytes(this uint value)
public static byte[] AsBytes(this uint value)
{
return BitConverter.GetBytes(value);
}
@ -53,7 +53,7 @@ namespace SweetLib.Utils.Extensions
/// </summary>
/// <param name="value">Value to convert.</param>
/// <returns>Byte array representation of the value.</returns>
public static byte[] ToBytes(this long value)
public static byte[] AsBytes(this long value)
{
return BitConverter.GetBytes(value);
}
@ -63,7 +63,7 @@ namespace SweetLib.Utils.Extensions
/// </summary>
/// <param name="value">Value to convert.</param>
/// <returns>Byte array representation of the value.</returns>
public static byte[] ToBytes(this ulong value)
public static byte[] AsBytes(this ulong value)
{
return BitConverter.GetBytes(value);
}
@ -73,7 +73,7 @@ namespace SweetLib.Utils.Extensions
/// </summary>
/// <param name="value">Value to convert.</param>
/// <returns>Byte array representation of the value.</returns>
public static byte[] ToBytes(this float value)
public static byte[] AsBytes(this float value)
{
return BitConverter.GetBytes(value);
}
@ -83,7 +83,7 @@ namespace SweetLib.Utils.Extensions
/// </summary>
/// <param name="value">Value to convert.</param>
/// <returns>Byte array representation of the value.</returns>
public static byte[] ToBytes(this double value)
public static byte[] AsBytes(this double value)
{
return BitConverter.GetBytes(value);
}
@ -93,7 +93,7 @@ namespace SweetLib.Utils.Extensions
/// </summary>
/// <param name="value">Value to convert.</param>
/// <returns>Byte array representation of the value.</returns>
public static byte[] ToBytes(this bool value)
public static byte[] AsBytes(this bool value)
{
return BitConverter.GetBytes(value);
}
@ -103,7 +103,7 @@ namespace SweetLib.Utils.Extensions
/// </summary>
/// <param name="value">Value to convert.</param>
/// <returns>Byte array representation of the value.</returns>
public static byte[] ToBytes(this DateTime value)
public static byte[] AsBytes(this DateTime value)
{
return BitConverter.GetBytes(value.ToUnixTimeStamp());
}
@ -114,17 +114,27 @@ namespace SweetLib.Utils.Extensions
/// <param name="value">Value to convert.</param>
/// <returns>Byte array representation of the value.</returns>
/// <remarks>Using <see cref="UTF32Encoding"/> for converting.</remarks>
public static byte[] ToBytes(this string value)
public static byte[] AsBytes(this string value)
{
return Encoding.UTF32.GetBytes(value);
}
/// <summary>
/// Converts a <see cref="char"/> value into a byte array.
/// </summary>
/// <param name="value">Value to convert.</param>
/// <returns>Byte array representation of the value.</returns>
public static byte[] AsBytes(this char value)
{
return BitConverter.GetBytes(value);
}
/// <summary>
/// Converts a <see cref="byte[]"/> into a <see cref="short"/> type.
/// </summary>
/// <param name="bytes">Byte array representation to convert.</param>
/// <returns>Converted value.</returns>
public static short ToInt16(this byte[] bytes)
public static short AsInt16(this byte[] bytes)
{
return BitConverter.ToInt16(bytes, 0);
}
@ -134,7 +144,7 @@ namespace SweetLib.Utils.Extensions
/// </summary>
/// <param name="bytes">Byte array representation to convert.</param>
/// <returns>Converted value.</returns>
public static ushort ToUInt16(this byte[] bytes)
public static ushort AsUInt16(this byte[] bytes)
{
return BitConverter.ToUInt16(bytes, 0);
}
@ -144,7 +154,7 @@ namespace SweetLib.Utils.Extensions
/// </summary>
/// <param name="bytes">Byte array representation to convert.</param>
/// <returns>Converted value.</returns>
public static int ToInt32(this byte[] bytes)
public static int AsInt32(this byte[] bytes)
{
return BitConverter.ToInt32(bytes, 0);
}
@ -154,7 +164,7 @@ namespace SweetLib.Utils.Extensions
/// </summary>
/// <param name="bytes">Byte array representation to convert.</param>
/// <returns>Converted value.</returns>
public static uint ToUInt32(this byte[] bytes)
public static uint AsUInt32(this byte[] bytes)
{
return BitConverter.ToUInt32(bytes, 0);
}
@ -164,7 +174,7 @@ namespace SweetLib.Utils.Extensions
/// </summary>
/// <param name="bytes">Byte array representation to convert.</param>
/// <returns>Converted value.</returns>
public static long ToInt64(this byte[] bytes)
public static long AsInt64(this byte[] bytes)
{
return BitConverter.ToInt64(bytes, 0);
}
@ -174,7 +184,7 @@ namespace SweetLib.Utils.Extensions
/// </summary>
/// <param name="bytes">Byte array representation to convert.</param>
/// <returns>Converted value.</returns>
public static ulong ToUInt64(this byte[] bytes)
public static ulong AsUInt64(this byte[] bytes)
{
return BitConverter.ToUInt64(bytes, 0);
}
@ -184,7 +194,7 @@ namespace SweetLib.Utils.Extensions
/// </summary>
/// <param name="bytes">Byte array representation to convert.</param>
/// <returns>Converted value.</returns>
public static float ToSingle(this byte[] bytes)
public static float AsSingle(this byte[] bytes)
{
return BitConverter.ToSingle(bytes, 0);
}
@ -194,20 +204,50 @@ namespace SweetLib.Utils.Extensions
/// </summary>
/// <param name="bytes">Byte array representation to convert.</param>
/// <returns>Converted value.</returns>
public static double ToDouble(this byte[] bytes)
public static double AsDouble(this byte[] bytes)
{
return BitConverter.ToDouble(bytes, 0);
}
/// <summary>
/// Converts a <see cref="byte[]"/> into a <see cref="bool"/> type.
/// </summary>
/// <param name="bytes">Byte array representation to convert.</param>
/// <returns>Converted value.</returns>
public static bool AsBool(this byte[] bytes)
{
return BitConverter.ToBoolean(bytes, 0);
}
/// <summary>
/// Converts a <see cref="byte[]"/> into a <see cref="DateTime"/> type.
/// </summary>
/// <param name="bytes">Byte array representation to convert.</param>
/// <returns>Converted value.</returns>
public static DateTime AsDateTime(this byte[] bytes)
{
return BitConverter.ToDouble(bytes, 0).ToDateTime();
}
/// <summary>
/// Converts a <see cref="byte[]"/> into a <see cref="string"/> type.
/// </summary>
/// <param name="bytes">Byte array representation to convert.</param>
/// <returns>Converted value.</returns>
/// <remarks>Using <see cref="UTF32Encoding"/> for converting.</remarks>
public static string ToString(this byte[] bytes)
public static string AsString(this byte[] bytes)
{
return Encoding.UTF32.GetString(bytes);
}
/// <summary>
/// Converts a <see cref="byte[]"/> into a <see cref="char"/> type.
/// </summary>
/// <param name="bytes">Byte array representation to convert.</param>
/// <returns>Converted value.</returns>
public static char AsChar(this byte[] bytes)
{
return BitConverter.ToChar(bytes, 0);
}
}
}