using System;
using System.Collections.Generic;
+using System.Linq;
using System.Text;
namespace Novacoin
{
- public class InteropException : Exception
+ /// <summary>
+ /// Miscellaneous functions
+ /// </summary>
+ public class Interop
{
- public InteropException()
+ /// <summary>
+ /// Convert array of unsigned integers to array of bytes.
+ /// </summary>
+ /// <param name="values">Array of unsigned integer values.</param>
+ /// <returns>Byte array</returns>
+ public static byte[] LEBytes(uint[] values)
{
- }
+ var result = new byte[values.Length * sizeof(uint)];
+ Buffer.BlockCopy(values, 0, result, 0, result.Length);
- public InteropException(string message)
- : base(message)
- {
+ return result;
}
- public InteropException(string message, Exception inner)
- : base(message, inner)
+ /// <summary>
+ /// Convert byte array to array of unsigned integers.
+ /// </summary>
+ /// <param name="bytes">Byte array.</param>
+ /// <returns>Array of integers</returns>
+ public static uint[] ToUInt32Array(byte[] bytes)
{
+ var result = new uint[bytes.Length / sizeof(uint)];
+ Buffer.BlockCopy(bytes, 0, result, 0, bytes.Length);
+
+ return result;
}
- }
- public class Interop
- {
+ /// <summary>
+ /// Reverse byte array
+ /// </summary>
+ /// <param name="source">Source array</param>
+ /// <returns>Result array</returns>
public static byte[] ReverseBytes(byte[] source)
{
var b = new byte[source.Length];
return b;
}
- public static byte[] LEBytes(uint[] values)
- {
- var result = new byte[values.Length * sizeof(uint)];
- Buffer.BlockCopy(values, 0, result, 0, result.Length);
-
- return result;
- }
-
- public static uint[] ToUInt32Array(byte[] bytes)
- {
- var result = new uint[bytes.Length / sizeof(uint)];
- Buffer.BlockCopy(bytes, 0, result, 0, bytes.Length);
-
- return result;
- }
-
public static byte[] HexToArray(string hex)
{
int nChars = hex.Length;
return bytes;
}
- public static string ToHex(IEnumerable<byte> 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();
foreach (var b in bytes)
}
return sb.ToString();
}
+
+ public static uint GetTime()
+ {
+ return (uint)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds;
+ }
}
}