using System;
using System.Collections.Generic;
-using System.Linq;
using System.Text;
namespace Novacoin
{
- public class InteropException : Exception
- {
- public InteropException()
- {
- }
-
- public InteropException(string message)
- : base(message)
- {
- }
-
- public InteropException(string message, Exception inner)
- : base(message, inner)
- {
- }
- }
-
+ /// <summary>
+ /// Miscellaneous functions
+ /// </summary>
public class Interop
{
- public static byte[] ReverseBytes(byte[] source)
- {
- var b = new byte[source.Length];
-
- source.CopyTo(b, 0);
-
- Array.Reverse(b);
-
- return b;
- }
-
+ /// <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)];
return result;
}
+ /// <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)];
return result;
}
- public static byte[] BEBytes(ushort n)
- {
- var resultBytes = BitConverter.GetBytes(n);
-
- Array.Reverse(resultBytes);
-
- return resultBytes;
- }
-
- public static byte[] BEBytes(uint n)
+ /// <summary>
+ /// Reverse byte array
+ /// </summary>
+ /// <param name="source">Source array</param>
+ /// <returns>Result array</returns>
+ public static byte[] ReverseBytes(byte[] source)
{
- var resultBytes = BitConverter.GetBytes(n);
-
- Array.Reverse(resultBytes);
-
- return resultBytes;
- }
+ var b = new byte[source.Length];
- public static byte[] BEBytes(ulong n)
- {
- var resultBytes = BitConverter.GetBytes(n);
+ source.CopyTo(b, 0);
- Array.Reverse(resultBytes);
+ Array.Reverse(b);
- return resultBytes;
+ return b;
}
-
- public static ushort BEBytesToUInt16(byte[] bytes)
+ public static byte[] HexToArray(string hex)
{
- if (bytes.Length != sizeof(ushort))
- {
- throw new InteropException("Array size doesn't match the ushort data type.");
- }
+ int nChars = hex.Length;
+ var bytes = new byte[nChars / 2];
- Array.Reverse(bytes);
-
- return BitConverter.ToUInt16(bytes, 0);
- }
-
- public static uint BEBytesToUInt32(byte[] bytes)
- {
- if (bytes.Length != sizeof(uint))
+ for (int i = 0; i < nChars; i += 2)
{
- throw new InteropException("Array size doesn't match the uint data type.");
+ bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
}
- Array.Reverse(bytes);
-
- return BitConverter.ToUInt32(bytes, 0);
+ return bytes;
}
- public static ulong BEBytesToUInt64(byte[] bytes)
+ public static byte[] TrimArray(byte[] bytes)
{
- if (bytes.Length != sizeof(ulong))
+ int trimStart = bytes.Length - 1;
+ while (trimStart >= 0 && bytes[trimStart] == 0)
{
- throw new InteropException("Array size doesn't match the ulong data type.");
+ trimStart--;
}
- Array.Reverse(bytes);
+ byte[] result = new byte[trimStart + 1];
+ Array.Copy(bytes, 0, result, 0, trimStart + 1);
- return BitConverter.ToUInt64(bytes, 0);
- }
-
- public static IEnumerable<byte> HexToEnumerable(string hex)
- {
- return Enumerable.Range(0, hex.Length)
- .Where(x => x % 2 == 0)
- .Select(x => Convert.ToByte(hex.Substring(x, 2), 16));
- }
-
- public static IList<byte> HexToList(string hex)
- {
- return HexToEnumerable(hex).ToList();
- }
-
- public static byte[] HexToArray(string hex)
- {
- return HexToEnumerable(hex).ToArray();
+ return result;
}
- public static string ToHex(IEnumerable<byte> bytes)
+ 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;
+ }
}
}