Bytes extension methods
This commit is contained in:
@@ -0,0 +1,159 @@
|
||||
namespace Common.Utils.Extensions
|
||||
{
|
||||
using System;
|
||||
|
||||
public static class BytesExtensions
|
||||
{
|
||||
public const Int32 Int16Size = sizeof(Int16);
|
||||
public const Int32 Int32Size = sizeof(Int32);
|
||||
public const Int32 Int64Size = sizeof(Int64);
|
||||
|
||||
public const Int32 UInt16Size = sizeof(UInt16);
|
||||
public const Int32 UInt32Size = sizeof(UInt32);
|
||||
public const Int32 UInt64Size = sizeof(UInt64);
|
||||
|
||||
public const Int32 SingleSize = sizeof(Single);
|
||||
public const Int32 DoubleSize = sizeof(Double);
|
||||
|
||||
public static Byte[] GetBits(this Byte _byte)
|
||||
{
|
||||
var bits = new Byte[8];
|
||||
|
||||
for (var i = 0; i < 8; i++)
|
||||
{
|
||||
bits[i] = (Byte)((_byte >> i) & 1);
|
||||
}
|
||||
|
||||
return bits;
|
||||
}
|
||||
|
||||
public static void SetBits(this Byte value, Byte[] bits, Int32 start, Int32 count)
|
||||
{
|
||||
for (var i = 0; i < count; i++)
|
||||
{
|
||||
bits[start + i] = (Byte)((value >> i) & 1);
|
||||
}
|
||||
}
|
||||
|
||||
public static Byte ToByte(this Byte[] bits)
|
||||
{
|
||||
var _byte = default(Int32);
|
||||
|
||||
for (var i = 0; i < 8; i++)
|
||||
{
|
||||
_byte |= bits[i] << i;
|
||||
}
|
||||
|
||||
return (Byte)_byte;
|
||||
}
|
||||
|
||||
public static Byte ToByte(this Byte[] bits, Int32 start, Int32 count)
|
||||
{
|
||||
var sum = 0;
|
||||
|
||||
for (var i = 0; i < count; i++)
|
||||
{
|
||||
sum |= bits[start + i] << i;
|
||||
}
|
||||
|
||||
return (Byte)sum;
|
||||
}
|
||||
|
||||
public static Byte[] GetReversed(this Byte[] bytes, Int32 start, Int32 size)
|
||||
{
|
||||
var _bytes = new Byte[size];
|
||||
|
||||
Array.Copy(bytes, start, _bytes, 0, size);
|
||||
Array.Reverse(_bytes);
|
||||
|
||||
return _bytes;
|
||||
}
|
||||
|
||||
public static Int16 ToInt16BigEndian(this Byte[] bytes, Int32 start)
|
||||
{
|
||||
var _bytes = bytes.GetReversed(start, Int16Size);
|
||||
|
||||
return BitConverter.ToInt16(_bytes, 0);
|
||||
}
|
||||
|
||||
public static Int32 ToInt32BigEndian(this Byte[] bytes, Int32 start)
|
||||
{
|
||||
var _bytes = bytes.GetReversed(start, Int32Size);
|
||||
|
||||
return BitConverter.ToInt32(_bytes, 0);
|
||||
}
|
||||
|
||||
public static Int64 ToInt64BigEndian(this Byte[] bytes, Int32 start)
|
||||
{
|
||||
var _bytes = bytes.GetReversed(start, Int64Size);
|
||||
|
||||
return BitConverter.ToInt64(_bytes, 0);
|
||||
}
|
||||
|
||||
public static UInt16 ToUInt16BigEndian(this Byte[] bytes, Int32 start)
|
||||
{
|
||||
var _bytes = bytes.GetReversed(start, UInt16Size);
|
||||
|
||||
return BitConverter.ToUInt16(_bytes, 0);
|
||||
}
|
||||
|
||||
public static UInt32 ToUInt32BigEndian(this Byte[] bytes, Int32 start)
|
||||
{
|
||||
var _bytes = bytes.GetReversed(start, UInt32Size);
|
||||
|
||||
return BitConverter.ToUInt32(_bytes, 0);
|
||||
}
|
||||
|
||||
public static UInt64 ToUInt64BigEndian(this Byte[] bytes, Int32 start)
|
||||
{
|
||||
var _bytes = bytes.GetReversed(start, UInt64Size);
|
||||
|
||||
return BitConverter.ToUInt64(_bytes, 0);
|
||||
}
|
||||
|
||||
public static Single ToSingleBigEndian(this Byte[] bytes, Int32 start)
|
||||
{
|
||||
var _bytes = bytes.GetReversed(start, SingleSize);
|
||||
|
||||
return BitConverter.ToSingle(_bytes, 0);
|
||||
}
|
||||
|
||||
public static Double ToDobleBigEndian(this Byte[] bytes, Int32 start)
|
||||
{
|
||||
var _bytes = bytes.GetReversed(start, DoubleSize);
|
||||
|
||||
return BitConverter.ToDouble(_bytes, 0);
|
||||
}
|
||||
|
||||
public static Byte[] Reverse(this Byte[] bytes)
|
||||
{
|
||||
Array.Reverse(bytes);
|
||||
|
||||
return bytes;
|
||||
}
|
||||
|
||||
public static Byte[] ToBigEndianBytes(this Int16 value)
|
||||
=> BitConverter.GetBytes(value).Reverse();
|
||||
|
||||
public static Byte[] ToBigEndianBytes(this Int32 value)
|
||||
=> BitConverter.GetBytes(value).Reverse();
|
||||
|
||||
public static Byte[] ToBigEndianBytes(this Int64 value)
|
||||
=> BitConverter.GetBytes(value).Reverse();
|
||||
|
||||
public static Byte[] ToBigEndianBytes(this UInt16 value)
|
||||
=> BitConverter.GetBytes(value).Reverse();
|
||||
|
||||
public static Byte[] ToBigEndianBytes(this UInt32 value)
|
||||
=> BitConverter.GetBytes(value).Reverse();
|
||||
|
||||
public static Byte[] ToBigEndianBytes(this UInt64 value)
|
||||
=> BitConverter.GetBytes(value).Reverse();
|
||||
|
||||
public static Byte[] ToBigEndianBytes(this Single value)
|
||||
=> BitConverter.GetBytes(value).Reverse();
|
||||
|
||||
public static Byte[] ToBigEndianBytes(this Double value)
|
||||
=> BitConverter.GetBytes(value).Reverse();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
|
||||
<PropertyGroup>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||
<ProjectGuid>{05F0CCAF-9F2B-45F9-A7F8-12F29AD182F3}</ProjectGuid>
|
||||
<OutputType>Library</OutputType>
|
||||
<RootNamespace>Common.Utils.Extensions</RootNamespace>
|
||||
<AssemblyName>Common.Utils.Extensions</AssemblyName>
|
||||
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
|
||||
<FileAlignment>512</FileAlignment>
|
||||
<Deterministic>true</Deterministic>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<PlatformTarget>AnyCPU</PlatformTarget>
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<DebugType>full</DebugType>
|
||||
<Optimize>false</Optimize>
|
||||
<OutputPath>bin\Debug\</OutputPath>
|
||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||
<PlatformTarget>AnyCPU</PlatformTarget>
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<Optimize>true</Optimize>
|
||||
<OutputPath>bin\Release\</OutputPath>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup>
|
||||
<StartupObject />
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="System.Xml.Linq" />
|
||||
<Reference Include="System.Data.DataSetExtensions" />
|
||||
<Reference Include="Microsoft.CSharp" />
|
||||
<Reference Include="System.Data" />
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="BytesExtensions.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
</Project>
|
||||
@@ -0,0 +1,36 @@
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
// General Information about an assembly is controlled through the following
|
||||
// set of attributes. Change these attribute values to modify the information
|
||||
// associated with an assembly.
|
||||
[assembly: AssemblyTitle("Common.Utils.Extensions")]
|
||||
[assembly: AssemblyDescription("")]
|
||||
[assembly: AssemblyConfiguration("")]
|
||||
[assembly: AssemblyCompany("")]
|
||||
[assembly: AssemblyProduct("Common.Utils.Extensions")]
|
||||
[assembly: AssemblyCopyright("Copyright © 2024")]
|
||||
[assembly: AssemblyTrademark("")]
|
||||
[assembly: AssemblyCulture("")]
|
||||
|
||||
// Setting ComVisible to false makes the types in this assembly not visible
|
||||
// to COM components. If you need to access a type in this assembly from
|
||||
// COM, set the ComVisible attribute to true on that type.
|
||||
[assembly: ComVisible(false)]
|
||||
|
||||
// The following GUID is for the ID of the typelib if this project is exposed to COM
|
||||
[assembly: Guid("05f0ccaf-9f2b-45f9-a7f8-12f29ad182f3")]
|
||||
|
||||
// Version information for an assembly consists of the following four values:
|
||||
//
|
||||
// Major Version
|
||||
// Minor Version
|
||||
// Build Number
|
||||
// Revision
|
||||
//
|
||||
// You can specify all the values or you can default the Build and Revision Numbers
|
||||
// by using the '*' as shown below:
|
||||
// [assembly: AssemblyVersion("1.0.*")]
|
||||
[assembly: AssemblyVersion("1.0.0.0")]
|
||||
[assembly: AssemblyFileVersion("1.0.0.0")]
|
||||
Reference in New Issue
Block a user