From 350080987fa0d348deeaf265674f49accf50e493 Mon Sep 17 00:00:00 2001 From: sebastian-heinz Date: Thu, 3 Mar 2022 19:40:50 +0800 Subject: [PATCH] fix double reading endianess issue --- .github/workflows/build.yaml | 2 +- .github/workflows/release.yml | 2 +- .version | 2 +- .../Arrowgene.Buffers.Test.csproj | 24 +++++++++++++++++++ Arrowgene.Buffers.Test/TestStreamBuffer.cs | 22 +++++++++++++++++ Arrowgene.Buffers.sln | 14 +++++++++++ Arrowgene.Buffers/EndiannessSwapper.cs | 24 +++++-------------- 7 files changed, 69 insertions(+), 21 deletions(-) create mode 100644 Arrowgene.Buffers.Test/Arrowgene.Buffers.Test.csproj create mode 100644 Arrowgene.Buffers.Test/TestStreamBuffer.cs diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index fe2f130..47f322a 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -8,6 +8,6 @@ jobs: - name: Setup .NET Core uses: actions/setup-dotnet@v1 with: - dotnet-version: 3.1.100 + dotnet-version: 6.0.200 - name: Build Project run: dotnet build --configuration Release \ No newline at end of file diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 47931a4..47d124f 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -14,7 +14,7 @@ jobs: - name: Setup .NET Core uses: actions/setup-dotnet@v1 with: - dotnet-version: 3.1.100 + dotnet-version: 6.0.200 - name: Build Release id: build_release diff --git a/.version b/.version index 7f20734..e6d5cb8 100644 --- a/.version +++ b/.version @@ -1 +1 @@ -1.0.1 \ No newline at end of file +1.0.2 \ No newline at end of file diff --git a/Arrowgene.Buffers.Test/Arrowgene.Buffers.Test.csproj b/Arrowgene.Buffers.Test/Arrowgene.Buffers.Test.csproj new file mode 100644 index 0000000..1cd44c3 --- /dev/null +++ b/Arrowgene.Buffers.Test/Arrowgene.Buffers.Test.csproj @@ -0,0 +1,24 @@ + + + + net6.0 + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + + + + diff --git a/Arrowgene.Buffers.Test/TestStreamBuffer.cs b/Arrowgene.Buffers.Test/TestStreamBuffer.cs new file mode 100644 index 0000000..4198cb8 --- /dev/null +++ b/Arrowgene.Buffers.Test/TestStreamBuffer.cs @@ -0,0 +1,22 @@ +using System; +using Xunit; + +namespace Arrowgene.Buffers.Test; + +public class TestStreamBuffer +{ + [Fact] + public void TestDoubleBigEndian() + { + byte[] dBytesIn = new byte[] {0xC0, 0xEA, 0x6B, 0xAC, 0xEC, 0x00, 0x00, 0x00}; + byte[] dBytesExp = new byte[] {0xC0, 0xEA, 0x6B, 0xAC, 0xEC, 0x00, 0x00, 0x00}; + Array.Reverse(dBytesExp); + double dExp = BitConverter.ToDouble(dBytesExp); + + StreamBuffer b = new StreamBuffer(dBytesIn); + b.SetPositionStart(); + double dOut = b.ReadDouble(Endianness.Big); + + Assert.Equal(dExp, dOut); + } +} \ No newline at end of file diff --git a/Arrowgene.Buffers.sln b/Arrowgene.Buffers.sln index 8755934..80e6020 100644 --- a/Arrowgene.Buffers.sln +++ b/Arrowgene.Buffers.sln @@ -5,6 +5,8 @@ VisualStudioVersion = 15.0.26124.0 MinimumVisualStudioVersion = 15.0.26124.0 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Arrowgene.Buffers", "Arrowgene.Buffers\Arrowgene.Buffers.csproj", "{5ECAC55A-187D-4834-993A-EB261E78DF3A}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Arrowgene.Buffers.Test", "Arrowgene.Buffers.Test\Arrowgene.Buffers.Test.csproj", "{E48D5E15-9C6A-4FA3-A82E-E92DFD6B5114}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -30,5 +32,17 @@ Global {5ECAC55A-187D-4834-993A-EB261E78DF3A}.Release|x64.Build.0 = Release|x64 {5ECAC55A-187D-4834-993A-EB261E78DF3A}.Release|x86.ActiveCfg = Release|x86 {5ECAC55A-187D-4834-993A-EB261E78DF3A}.Release|x86.Build.0 = Release|x86 + {E48D5E15-9C6A-4FA3-A82E-E92DFD6B5114}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E48D5E15-9C6A-4FA3-A82E-E92DFD6B5114}.Debug|Any CPU.Build.0 = Debug|Any CPU + {E48D5E15-9C6A-4FA3-A82E-E92DFD6B5114}.Debug|x64.ActiveCfg = Debug|Any CPU + {E48D5E15-9C6A-4FA3-A82E-E92DFD6B5114}.Debug|x64.Build.0 = Debug|Any CPU + {E48D5E15-9C6A-4FA3-A82E-E92DFD6B5114}.Debug|x86.ActiveCfg = Debug|Any CPU + {E48D5E15-9C6A-4FA3-A82E-E92DFD6B5114}.Debug|x86.Build.0 = Debug|Any CPU + {E48D5E15-9C6A-4FA3-A82E-E92DFD6B5114}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E48D5E15-9C6A-4FA3-A82E-E92DFD6B5114}.Release|Any CPU.Build.0 = Release|Any CPU + {E48D5E15-9C6A-4FA3-A82E-E92DFD6B5114}.Release|x64.ActiveCfg = Release|Any CPU + {E48D5E15-9C6A-4FA3-A82E-E92DFD6B5114}.Release|x64.Build.0 = Release|Any CPU + {E48D5E15-9C6A-4FA3-A82E-E92DFD6B5114}.Release|x86.ActiveCfg = Release|Any CPU + {E48D5E15-9C6A-4FA3-A82E-E92DFD6B5114}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection EndGlobal diff --git a/Arrowgene.Buffers/EndiannessSwapper.cs b/Arrowgene.Buffers/EndiannessSwapper.cs index 59ec387..10bad9b 100644 --- a/Arrowgene.Buffers/EndiannessSwapper.cs +++ b/Arrowgene.Buffers/EndiannessSwapper.cs @@ -36,28 +36,16 @@ public ulong SwapBytes(ulong x) public float SwapBytes(float input) { - byte[] tmpIn = BitConverter.GetBytes(input); - byte[] tmpOut = new byte[4]; - tmpOut[0] = tmpIn[3]; - tmpOut[1] = tmpIn[2]; - tmpOut[2] = tmpIn[1]; - tmpOut[3] = tmpIn[0]; - return BitConverter.ToSingle(tmpOut, 0); + byte[] tmp = BitConverter.GetBytes(input); + Array.Reverse(tmp); + return BitConverter.ToSingle(tmp, 0); } public double SwapBytes(double input) { - byte[] tmpIn = BitConverter.GetBytes(input); - byte[] tmpOut = new byte[8]; - tmpOut[0] = tmpIn[7]; - tmpOut[1] = tmpIn[6]; - tmpOut[2] = tmpIn[5]; - tmpOut[3] = tmpIn[4]; - tmpOut[4] = tmpIn[3]; - tmpOut[5] = tmpIn[2]; - tmpOut[6] = tmpIn[1]; - tmpOut[7] = tmpIn[0]; - return BitConverter.ToSingle(tmpOut, 0); + byte[] tmp = BitConverter.GetBytes(input); + Array.Reverse(tmp); + return BitConverter.ToDouble(tmp, 0); } public short SwapBytes(short value)