diff --git a/BitConverter.sln b/BitConverter.sln index ac7865a..aab2dc5 100644 --- a/BitConverter.sln +++ b/BitConverter.sln @@ -1,11 +1,20 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 15 -VisualStudioVersion = 15.0.26228.4 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.30104.148 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EndianBitConverter", "EndianBitConverter\EndianBitConverter.csproj", "{848519BA-2143-4B62-92B4-A93425CE8A19}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EndianBitConverter", "EndianBitConverter\EndianBitConverter.csproj", "{ABC12A77-84A8-4079-995A-E72FE047CC96}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EndianBitConverter.Tests", "EndianBitConverter.Tests\EndianBitConverter.Tests.csproj", "{8D662449-7772-4CD2-9444-C073B6FE1F90}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EndianBitConverter.Tests", "EndianBitConverter.Tests\EndianBitConverter.Tests.csproj", "{C56FC316-F02D-4C0D-B588-CAA4132CF360}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "_solution files", "_solution files", "{C7D9E938-1285-4249-B43F-7D1F4DEA9FA1}" + ProjectSection(SolutionItems) = preProject + .gitattributes = .gitattributes + .gitignore = .gitignore + EndianBitConverter.png = EndianBitConverter.png + LICENSE.txt = LICENSE.txt + README.md = README.md + EndProjectSection EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -13,16 +22,19 @@ Global Release|Any CPU = Release|Any CPU EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution - {848519BA-2143-4B62-92B4-A93425CE8A19}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {848519BA-2143-4B62-92B4-A93425CE8A19}.Debug|Any CPU.Build.0 = Debug|Any CPU - {848519BA-2143-4B62-92B4-A93425CE8A19}.Release|Any CPU.ActiveCfg = Release|Any CPU - {848519BA-2143-4B62-92B4-A93425CE8A19}.Release|Any CPU.Build.0 = Release|Any CPU - {8D662449-7772-4CD2-9444-C073B6FE1F90}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {8D662449-7772-4CD2-9444-C073B6FE1F90}.Debug|Any CPU.Build.0 = Debug|Any CPU - {8D662449-7772-4CD2-9444-C073B6FE1F90}.Release|Any CPU.ActiveCfg = Release|Any CPU - {8D662449-7772-4CD2-9444-C073B6FE1F90}.Release|Any CPU.Build.0 = Release|Any CPU + {ABC12A77-84A8-4079-995A-E72FE047CC96}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {ABC12A77-84A8-4079-995A-E72FE047CC96}.Debug|Any CPU.Build.0 = Debug|Any CPU + {ABC12A77-84A8-4079-995A-E72FE047CC96}.Release|Any CPU.ActiveCfg = Release|Any CPU + {ABC12A77-84A8-4079-995A-E72FE047CC96}.Release|Any CPU.Build.0 = Release|Any CPU + {C56FC316-F02D-4C0D-B588-CAA4132CF360}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C56FC316-F02D-4C0D-B588-CAA4132CF360}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C56FC316-F02D-4C0D-B588-CAA4132CF360}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C56FC316-F02D-4C0D-B588-CAA4132CF360}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {5ECCA5D4-B184-46AC-A898-61EAE3F91B47} + EndGlobalSection EndGlobal diff --git a/EndianBitConverter.Tests/EndianBitConverter.Tests.csproj b/EndianBitConverter.Tests/EndianBitConverter.Tests.csproj index 5e37fcf..a345ee8 100644 --- a/EndianBitConverter.Tests/EndianBitConverter.Tests.csproj +++ b/EndianBitConverter.Tests/EndianBitConverter.Tests.csproj @@ -1,22 +1,19 @@  - - net45;netcoreapp1.0 - False - + + netcoreapp3.1 + false + - - - - - + + + + + + - - - - - - - + + + diff --git a/EndianBitConverter.Tests/GetBytesTests.cs b/EndianBitConverter.Tests/GetBytesTests.cs index a7abd9e..b954d43 100644 --- a/EndianBitConverter.Tests/GetBytesTests.cs +++ b/EndianBitConverter.Tests/GetBytesTests.cs @@ -1,6 +1,6 @@ // Copyright (C) Microsoft Corporation. All rights reserved. -namespace BitConverter.Tests +namespace EndianBitConverter.Tests { using System; using System.Linq; diff --git a/EndianBitConverter.Tests/GetValueTests.cs b/EndianBitConverter.Tests/GetValueTests.cs index 4584d26..817c5f0 100644 --- a/EndianBitConverter.Tests/GetValueTests.cs +++ b/EndianBitConverter.Tests/GetValueTests.cs @@ -1,6 +1,6 @@ // Copyright (C) Microsoft Corporation. All rights reserved. -namespace BitConverter.Tests +namespace EndianBitConverter.Tests { using System; using System.Linq; diff --git a/EndianBitConverter.Tests/InterfaceTests.cs b/EndianBitConverter.Tests/InterfaceTests.cs index 0aa9735..92cff3e 100644 --- a/EndianBitConverter.Tests/InterfaceTests.cs +++ b/EndianBitConverter.Tests/InterfaceTests.cs @@ -1,6 +1,6 @@ // Copyright (C) Microsoft Corporation. All rights reserved. -namespace BitConverter.Tests +namespace EndianBitConverter.Tests { using System.Reflection; using Microsoft.VisualStudio.TestTools.UnitTesting; @@ -11,8 +11,21 @@ public class InterfaceTests [TestMethod] public void IsLittleEndian() { + //big-endian Assert.IsFalse(EndianBitConverter.BigEndian.IsLittleEndian); + Assert.IsFalse(EndianBitConverter.BigEndian.IsMid); + + //mid big-endian + Assert.IsFalse(EndianBitConverter.MidBigEndian.IsLittleEndian); + Assert.IsTrue(EndianBitConverter.MidBigEndian.IsMid); + + //little-endian Assert.IsTrue(EndianBitConverter.LittleEndian.IsLittleEndian); + Assert.IsFalse(EndianBitConverter.LittleEndian.IsMid); + + //mid little-endian + Assert.IsTrue(EndianBitConverter.MidLittleEndian.IsLittleEndian); + Assert.IsTrue(EndianBitConverter.MidLittleEndian.IsMid); } } } diff --git a/EndianBitConverter.Tests/Properties/AssemblyInfo.cs b/EndianBitConverter.Tests/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..e2dfbf5 --- /dev/null +++ b/EndianBitConverter.Tests/Properties/AssemblyInfo.cs @@ -0,0 +1,20 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +[assembly: AssemblyTitle("EndianBitConverter.Tests")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("EndianBitConverter.Tests")] +[assembly: AssemblyCopyright("Copyright © 2020")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +[assembly: ComVisible(false)] + +[assembly: Guid("12c7685a-9cba-4fd2-9679-6360fa7daed5")] + +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/EndianBitConverter/BigEndianBitConverter.cs b/EndianBitConverter/BigEndianBitConverter.cs index 4215d26..498fcd2 100644 --- a/EndianBitConverter/BigEndianBitConverter.cs +++ b/EndianBitConverter/BigEndianBitConverter.cs @@ -1,6 +1,6 @@ // Copyright (C) Microsoft Corporation. All rights reserved. -namespace BitConverter +namespace EndianBitConverter { /// /// A big-endian BitConverter that converts base data types to an array of bytes, and an array of bytes to base data types. All conversions are in @@ -12,7 +12,7 @@ internal class BigEndianBitConverter : EndianBitConverter internal BigEndianBitConverter() { } public override bool IsLittleEndian { get; } = false; - + public override bool IsMid { get; } = false; public override byte[] GetBytes(short value) { return new byte[] { (byte)(value >> 8), (byte)value }; diff --git a/EndianBitConverter/EndianBitConverter.cs b/EndianBitConverter/EndianBitConverter.cs index 62777fc..efbc5ff 100644 --- a/EndianBitConverter/EndianBitConverter.cs +++ b/EndianBitConverter/EndianBitConverter.cs @@ -1,6 +1,6 @@ // Copyright (C) Microsoft Corporation. All rights reserved. -namespace BitConverter +namespace EndianBitConverter { using System; using System.Runtime.CompilerServices; @@ -22,17 +22,35 @@ public abstract class EndianBitConverter /// public static EndianBitConverter LittleEndian { get; } = new LittleEndianBitConverter(); + /// + /// Get an instance of a , a BitConverter which performs all conversions in mid little-endian format regardless of + /// machine architecture. + /// + public static EndianBitConverter MidLittleEndian { get; } = new MidLittleEndianBitConverter(); + /// /// Get an instance of a , a BitConverter which performs all conversions in big-endian format regardless of /// machine architecture. /// public static EndianBitConverter BigEndian { get; } = new BigEndianBitConverter(); + /// + /// Get an instance of a , a BitConverter which performs all conversions in mid big-endian format regardless of + /// machine architecture. + /// + public static EndianBitConverter MidBigEndian { get; } = new MidBigEndianBitConverter(); + /// /// Indicates the byte order ("endianness") in which data should be converted. /// public abstract bool IsLittleEndian { get; } + /// + /// Indicates if the bytes should be converted from mid. + /// + public abstract bool IsMid { get; } + + /// /// Returns the specified Boolean value as a byte array. /// diff --git a/EndianBitConverter/EndianBitConverter.csproj b/EndianBitConverter/EndianBitConverter.csproj index 4ba8749..965e319 100644 --- a/EndianBitConverter/EndianBitConverter.csproj +++ b/EndianBitConverter/EndianBitConverter.csproj @@ -1,23 +1,19 @@ - + - net45;net40;netcoreapp1.0;netstandard1.0 - True - 1.1.0.0 - bin\$(Configuration)\$(TargetFramework)\$(MSBuildProjectName).xml - Microsoft Corporation - © Microsoft Corporation. All rights reserved. - David Reaburn - Endian BigEndian LittleEndian BitConverter - True - Provides a big-endian and little-endian BitConverter that convert base data types to an array of bytes, and an array of bytes to base data types, regardless of machine architecture. - https://raw.githubusercontent.com/davidrea-MS/BitConverter/master/LICENSE.txt - https://github.com/davidrea-MS/BitConverter - https://raw.githubusercontent.com/davidrea-MS/BitConverter/master/EndianBitConverter.png + netstandard2.0 + 3.1.0.0 + 3.1.0.0 + 3.1.0 + true + David Reaburn, Chris Leclair + LICENSE.txt + https://github.com/XCVG/BitConverter + EndianBitConverter.png + https://github.com/ovidiaconescu/BitConverter + © Microsoft Corporation, Chris Leclair, Ovidiu Diaconescu. All rights reserved. + Microsoft Corporation, XCVG Systems, Ovidiu Diaconescu + Provides a big-endian, little-endian mid big-endian, mid little-endian BitConverter that convert base data types to an array of bytes, and an array of bytes to base data types, regardless of machine architecture. This is a third-party fork of the original project with the project cleaned up slightly and updated to .NET Standard 2.0. + Endian BigEndian LittleEndian MidBigEndian MidLittleEndian BitConverter - - - - - - \ No newline at end of file + diff --git a/EndianBitConverter/LICENSE.EndianBitConverter.txt b/EndianBitConverter/LICENSE.EndianBitConverter.txt deleted file mode 100644 index 5d7c15d..0000000 --- a/EndianBitConverter/LICENSE.EndianBitConverter.txt +++ /dev/null @@ -1,10 +0,0 @@ -Copyright (C) Microsoft Corporation -All rights reserved. - -MIT License - -Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/EndianBitConverter/LittleEndianBitConverter.cs b/EndianBitConverter/LittleEndianBitConverter.cs index 91d2037..b0c9f86 100644 --- a/EndianBitConverter/LittleEndianBitConverter.cs +++ b/EndianBitConverter/LittleEndianBitConverter.cs @@ -1,6 +1,6 @@ // Copyright (C) Microsoft Corporation. All rights reserved. -namespace BitConverter +namespace EndianBitConverter { /// /// A little-endian BitConverter that converts base data types to an array of bytes, and an array of bytes to base data types. All conversions are in @@ -12,7 +12,7 @@ internal class LittleEndianBitConverter : EndianBitConverter internal LittleEndianBitConverter() { } public override bool IsLittleEndian { get; } = true; - + public override bool IsMid { get; } = false; public override byte[] GetBytes(short value) { return new byte[] { (byte)value, (byte)(value >> 8) }; diff --git a/EndianBitConverter/MidBigEndianBitConverter.cs b/EndianBitConverter/MidBigEndianBitConverter.cs new file mode 100644 index 0000000..718b5ca --- /dev/null +++ b/EndianBitConverter/MidBigEndianBitConverter.cs @@ -0,0 +1,59 @@ +// Copyright (C) Microsoft Corporation. All rights reserved. + +namespace EndianBitConverter +{ + /// + /// A mid big-endian BitConverter that converts base data types to an array of bytes, and an array of bytes to base data types. + /// All conversions are in mid big-endian format regardless of machine architecture. + /// + internal class MidBigEndianBitConverter : EndianBitConverter + { + // Instance available from EndianBitConverter.BigEndian + internal MidBigEndianBitConverter() { } + + public override bool IsLittleEndian { get; } = false; + public override bool IsMid { get; } = true; + + + public override byte[] GetBytes(short value) + { + return new byte[] { (byte)(value >> 8), (byte)value }; + } + + public override byte[] GetBytes(int value) + { + return new byte[] { (byte)(value >> 16), (byte)(value >> 24), (byte)value, (byte)(value >> 8) }; + } + + public override byte[] GetBytes(long value) + { + return new byte[] { + (byte)(value >> 48), (byte)(value >> 56), (byte)(value >> 32), (byte)(value >> 40), + (byte)(value >> 16), (byte)(value >> 24), (byte)value, (byte)(value >> 8) + }; + } + + public override short ToInt16(byte[] value, int startIndex) + { + this.CheckArguments(value, startIndex, sizeof(short)); + + return (short)((value[startIndex] << 8) | (value[startIndex + 1])); + } + + public override int ToInt32(byte[] value, int startIndex) + { + this.CheckArguments(value, startIndex, sizeof(int)); + + return (value[startIndex] << 16) | (value[startIndex + 1] << 24) | (value[startIndex + 2]) | (value[startIndex + 3] << 8); + } + + public override long ToInt64(byte[] value, int startIndex) + { + this.CheckArguments(value, startIndex, sizeof(long)); + + int highBytes = (value[startIndex] << 16) | (value[startIndex + 1] << 24) | (value[startIndex + 2]) | (value[startIndex + 3] << 8); + int lowBytes = (value[startIndex + 4] << 16) | (value[startIndex + 5] << 24) | (value[startIndex + 6]) | (value[startIndex + 7] << 8); + return ((uint)lowBytes | ((long)highBytes << 32)); + } + } +} diff --git a/EndianBitConverter/MidLittleEndianBitConverter.cs b/EndianBitConverter/MidLittleEndianBitConverter.cs new file mode 100644 index 0000000..6b73003 --- /dev/null +++ b/EndianBitConverter/MidLittleEndianBitConverter.cs @@ -0,0 +1,57 @@ +namespace EndianBitConverter +{ + /// + /// A mid little-endian BitConverter that converts base data types to an array of bytes, and an array of bytes to base data types. + /// All conversions are in mid little-endian format regardless of machine architecture. + /// + internal class MidLittleEndianBitConverter : EndianBitConverter + { + // Instance available from EndianBitConverter.MidLittleEndian + internal MidLittleEndianBitConverter() { } + + public override bool IsLittleEndian { get; } = true; + public override bool IsMid { get; } = true; + + + public override byte[] GetBytes(short value) + { + return new byte[] { (byte)value, (byte)(value >> 8) }; + } + + public override byte[] GetBytes(int value) + { + return new byte[] { (byte)(value >> 8), (byte)value, (byte)(value >> 24), (byte)(value >> 16) }; + } + + public override byte[] GetBytes(long value) + { + return new byte[] { + (byte)(value >> 8), (byte)value, (byte)(value >> 24), (byte)(value >> 16), + (byte)(value >> 40), (byte)(value >> 32), (byte)(value >> 56), (byte)(value >> 48) + }; + } + + public override short ToInt16(byte[] value, int startIndex) + { + this.CheckArguments(value, startIndex, sizeof(short)); + + return (short)((value[startIndex]) | (value[startIndex + 1] << 8)); + } + + public override int ToInt32(byte[] value, int startIndex) + { + this.CheckArguments(value, startIndex, sizeof(int)); + + return (value[startIndex] << 8) | (value[startIndex + 1]) | (value[startIndex + 2] << 24) | (value[startIndex + 3] << 16 ); + } + + public override long ToInt64(byte[] value, int startIndex) + { + this.CheckArguments(value, startIndex, sizeof(long)); + + int lowBytes = (value[startIndex] << 8) | (value[startIndex + 1]) | (value[startIndex + 2] << 24) | (value[startIndex + 3] << 16); + int highBytes = (value[startIndex + 4] << 8) | (value[startIndex + 5]) | (value[startIndex + 6] << 24) | (value[startIndex + 7] << 16); + return ((uint)lowBytes | ((long)highBytes << 32)); + } + } +} diff --git a/EndianBitConverter/SingleConverter.cs b/EndianBitConverter/SingleConverter.cs index 45f7e84..8c91ace 100644 --- a/EndianBitConverter/SingleConverter.cs +++ b/EndianBitConverter/SingleConverter.cs @@ -1,6 +1,6 @@ // Copyright (C) Microsoft Corporation. All rights reserved. -namespace BitConverter +namespace EndianBitConverter { using System.Runtime.InteropServices; diff --git a/LICENSE.txt b/LICENSE.txt index 5d7c15d..b306838 100644 --- a/LICENSE.txt +++ b/LICENSE.txt @@ -1,4 +1,5 @@ Copyright (C) Microsoft Corporation +Copyright (c) 2019 Chris Leclair All rights reserved. MIT License diff --git a/README.md b/README.md new file mode 100644 index 0000000..f5fa000 --- /dev/null +++ b/README.md @@ -0,0 +1,15 @@ +# EndianBitConverter + +Provides a big-endian and little-endian BitConverter that convert base data types to an array of bytes, and an array of bytes to base data types, regardless of machine architecture. + +This fork cleans up and updates a few things. It was really intended for one or two specific projects rather than a direct replacement for the original. + +## Changes in this fork + +* Updated project and solution files. They're created and maintained with Visual Studio 2019. +* Updated to .NET Standard 2.0 and test project to .NET Framework 4.8. This removes a bunch of unused dependencies when used with .NET Standard 2.0 compatible frameworks but obviously means it **no longer works with older frameworks.** +* Changed the namespace from "BitConverter" to "EndianBitConverter" as the former clashes with the built-in BitConverter type. **This is a breaking change.** + +## License + +This project is licensed under the MIT License. See the included LICENSE.txt for details. \ No newline at end of file