X-Git-Url: https://git.novaco.in/?a=blobdiff_plain;f=Novacoin%2FInterop.cs;h=f21c5c2f02a8df7fef72aee3a3320a007877dd77;hb=bcdf1637787154a7d20d06fefd235424fda1a1e4;hp=a2ead394a39ad063f8f34d7a89282e9089796ce9;hpb=e6724f8f1d99d872e84551affc1d9a820a7903d8;p=NovacoinLibrary.git diff --git a/Novacoin/Interop.cs b/Novacoin/Interop.cs index a2ead39..f21c5c2 100644 --- a/Novacoin/Interop.cs +++ b/Novacoin/Interop.cs @@ -24,6 +24,61 @@ namespace Novacoin public class Interop { + public static byte[] ReverseIfLE(byte[] source) + { + if (BitConverter.IsLittleEndian) + { + Array.Reverse(source); + } + + return source; + } + + public static byte[] LEBytes(uint[] values) + { + if (BitConverter.IsLittleEndian) + { + byte[] result = new byte[values.Length * sizeof(uint)]; + Buffer.BlockCopy(values, 0, result, 0, result.Length); + + return result; + } + else + { + List result = new List(); + + foreach (uint i in values) + { + result.AddRange(LEBytes(i)); + } + + return result.ToArray(); + } + } + + public static uint[] ToUInt32Array(byte[] bytes) + { + if (BitConverter.IsLittleEndian) + { + uint[] result = new uint[bytes.Length / sizeof(uint)]; + Buffer.BlockCopy(bytes, 0, result, 0, result.Length); + + return result; + } + else + { + List result = new List(); + + for (int i = 0; i < bytes.Length; i += 4) + { + result.Add(LEBytesToUInt32(bytes.Skip(i).Take(4).ToArray())); + } + + return result.ToArray(); + } + + } + public static byte[] LEBytes(ushort n) { byte[] resultBytes = BitConverter.GetBytes(n);