X-Git-Url: https://git.novaco.in/?a=blobdiff_plain;f=Novacoin%2FInterop.cs;h=54768cfe4a3f68f71df3cb9294d1a029a382ae17;hb=HEAD;hp=f4c1e2eec7c9b18ddda22ccfed5031b9bdad5339;hpb=6c6e3b0c68e764ea520dde48d5e44c757f1dabbb;p=NovacoinLibrary.git diff --git a/Novacoin/Interop.cs b/Novacoin/Interop.cs index f4c1e2e..54768cf 100644 --- a/Novacoin/Interop.cs +++ b/Novacoin/Interop.cs @@ -18,6 +18,7 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Text; namespace Novacoin @@ -28,6 +29,32 @@ namespace Novacoin public class Interop { /// + /// Convert array of unsigned integers to array of bytes. + /// + /// Array of unsigned integer values. + /// Byte array + public static byte[] LEBytes(uint[] values) + { + var result = new byte[values.Length * sizeof(uint)]; + Buffer.BlockCopy(values, 0, result, 0, result.Length); + + return result; + } + + /// + /// Convert byte array to array of unsigned integers. + /// + /// Byte array. + /// Array of integers + public static uint[] ToUInt32Array(byte[] bytes) + { + var result = new uint[bytes.Length / sizeof(uint)]; + Buffer.BlockCopy(bytes, 0, result, 0, bytes.Length); + + return result; + } + + /// /// Reverse byte array /// /// Source array @@ -56,6 +83,25 @@ namespace Novacoin return bytes; } + public static byte[] TrimArray(byte[] bytes) + { + int trimStart = bytes.Length - 1; + while (trimStart >= 0 && bytes[trimStart] == 0) + { + trimStart--; + } + + return bytes.Take(trimStart + 1).ToArray(); + } + + public static byte[] AppendWithZeros(byte[] bytes, int nTargetLen=32) + { + var result = new byte[nTargetLen]; + bytes.CopyTo(result, 0); + + return result; + } + public static string ToHex(byte[] bytes) { var sb = new StringBuilder();