Skip to content

Commit

Permalink
fix double reading endianess issue
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastian-heinz committed Mar 3, 2022
1 parent 8dc10a6 commit 3500809
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion .version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.0.1
1.0.2
24 changes: 24 additions & 0 deletions Arrowgene.Buffers.Test/Arrowgene.Buffers.Test.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.1.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Arrowgene.Buffers\Arrowgene.Buffers.csproj" />
</ItemGroup>

</Project>
22 changes: 22 additions & 0 deletions Arrowgene.Buffers.Test/TestStreamBuffer.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
14 changes: 14 additions & 0 deletions Arrowgene.Buffers.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
24 changes: 6 additions & 18 deletions Arrowgene.Buffers/EndiannessSwapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 3500809

Please sign in to comment.