X-Git-Url: https://git.novaco.in/?a=blobdiff_plain;f=Novacoin%2FInterop.cs;h=55979d714e103777f76978973329f8dbacea8d80;hb=eb39142ad873e102405ceffb5ec837b510da9096;hp=e3e4e6d1c38cf12b18cfff68bc27a54c835c1474;hpb=397772c7e6df61eaf558b3502ae403b7abfd86a2;p=NovacoinLibrary.git
diff --git a/Novacoin/Interop.cs b/Novacoin/Interop.cs
index e3e4e6d..55979d7 100644
--- a/Novacoin/Interop.cs
+++ b/Novacoin/Interop.cs
@@ -22,25 +22,42 @@ using System.Text;
namespace Novacoin
{
- public class InteropException : Exception
+ ///
+ /// Miscellaneous functions
+ ///
+ public class Interop
{
- public InteropException()
+ ///
+ /// 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);
- public InteropException(string message)
- : base(message)
- {
+ return result;
}
- public InteropException(string message, Exception inner)
- : base(message, inner)
+ ///
+ /// 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;
}
- }
- public class Interop
- {
+ ///
+ /// Reverse byte array
+ ///
+ /// Source array
+ /// Result array
public static byte[] ReverseBytes(byte[] source)
{
var b = new byte[source.Length];
@@ -52,22 +69,6 @@ namespace Novacoin
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;
@@ -81,7 +82,21 @@ namespace Novacoin
return bytes;
}
- public static string ToHex(IEnumerable bytes)
+ public static byte[] TrimArray(byte[] bytes)
+ {
+ int trimStart = bytes.Length - 1;
+ while (trimStart >= 0 && bytes[trimStart] == 0)
+ {
+ trimStart--;
+ }
+
+ byte[] result = new byte[trimStart + 1];
+ Array.Copy(bytes, 0, result, 0, trimStart + 1);
+
+ return result;
+ }
+
+ public static string ToHex(byte[] bytes)
{
var sb = new StringBuilder();
foreach (var b in bytes)
@@ -90,5 +105,10 @@ namespace Novacoin
}
return sb.ToString();
}
+
+ public static uint GetTime()
+ {
+ return (uint)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds;
+ }
}
}