Skip to content

Commit 5708d91

Browse files
committed
WIP
1 parent 532919d commit 5708d91

File tree

1 file changed

+16
-13
lines changed

1 file changed

+16
-13
lines changed

NBitcoin/UInt256.cs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
using NBitcoin.DataEncoders;
33
using System;
4+
using System.Buffers.Binary;
45
using System.Linq;
56
using System.Runtime.InteropServices;
67

@@ -174,25 +175,27 @@ public Span<char> ToSpanString()
174175
public void ToSpanString(Span<char> destination)
175176
{
176177
Span<ulong> ulongs = stackalloc ulong[4];
177-
ulongs[0] = pn0;
178-
ulongs[1] = pn1;
179-
ulongs[2] = pn2;
180-
ulongs[3] = pn3;
181-
Span<byte> bytes = MemoryMarshal.Cast<ulong, byte>(ulongs);
182178

183179
if (BitConverter.IsLittleEndian)
184180
{
185-
for (int i = 31, j = 0; i >= 0; i--, j += 2)
186-
{
187-
HexEncoder.ToCharsBuffer(bytes[i], destination, startingIndex: j);
188-
}
181+
ulongs[0] = pn0;
182+
ulongs[1] = pn1;
183+
ulongs[2] = pn2;
184+
ulongs[3] = pn3;
189185
}
190186
else
191187
{
192-
for (int i = 0; i < 32; i++)
193-
{
194-
HexEncoder.ToCharsBuffer(bytes[i], destination, startingIndex: i);
195-
}
188+
ulongs[0] = BinaryPrimitives.ReverseEndianness(pn0);
189+
ulongs[1] = BinaryPrimitives.ReverseEndianness(pn1);
190+
ulongs[2] = BinaryPrimitives.ReverseEndianness(pn2);
191+
ulongs[3] = BinaryPrimitives.ReverseEndianness(pn3);
192+
}
193+
194+
Span<byte> bytes = MemoryMarshal.Cast<ulong, byte>(ulongs);
195+
196+
for (int i = 31, j = 0; i >= 0; i--, j += 2)
197+
{
198+
HexEncoder.ToCharsBuffer(bytes[i], destination, startingIndex: j);
196199
}
197200
}
198201
#endif

0 commit comments

Comments
 (0)