X-Git-Url: https://git.novaco.in/?a=blobdiff_plain;f=Novacoin%2FInterop.cs;h=55979d714e103777f76978973329f8dbacea8d80;hb=eb39142ad873e102405ceffb5ec837b510da9096;hp=f0b2c9396404915d77e937f75f0861b8f550598f;hpb=4426ee1dc8ae6733d46b5413d3bce28333792d22;p=NovacoinLibrary.git diff --git a/Novacoin/Interop.cs b/Novacoin/Interop.cs index f0b2c93..55979d7 100644 --- a/Novacoin/Interop.cs +++ b/Novacoin/Interop.cs @@ -18,141 +18,97 @@ 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) - { - } - } - + /// + /// Miscellaneous functions + /// public class Interop { - public static byte[] ReverseBytes(byte[] source) - { - byte[] b = new byte[source.Length]; - - source.CopyTo(b, 0); - - Array.Reverse(b); - - return b; - } - + /// + /// Convert array of unsigned integers to array of bytes. + /// + /// Array of unsigned integer values. + /// Byte array public static byte[] LEBytes(uint[] values) { - byte[] result = new byte[values.Length * sizeof(uint)]; + 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) { - uint[] result = new uint[bytes.Length / sizeof(uint)]; + var result = new uint[bytes.Length / sizeof(uint)]; Buffer.BlockCopy(bytes, 0, result, 0, bytes.Length); return result; } - public static byte[] BEBytes(ushort n) + /// + /// Reverse byte array + /// + /// Source array + /// Result array + public static byte[] ReverseBytes(byte[] source) { - byte[] resultBytes = BitConverter.GetBytes(n); - - Array.Reverse(resultBytes); - - return resultBytes; - } + var b = new byte[source.Length]; - public static byte[] BEBytes(uint n) - { - byte[] resultBytes = BitConverter.GetBytes(n); + source.CopyTo(b, 0); - Array.Reverse(resultBytes); + Array.Reverse(b); - return resultBytes; + return b; } - public static byte[] BEBytes(ulong n) + public static byte[] HexToArray(string hex) { - byte[] resultBytes = BitConverter.GetBytes(n); - - Array.Reverse(resultBytes); - - return resultBytes; - } + int nChars = hex.Length; + var bytes = new byte[nChars / 2]; - - public static ushort BEBytesToUInt16(byte[] bytes) - { - if (bytes.Length != sizeof(ushort)) + for (int i = 0; i < nChars; i += 2) { - throw new InteropException("Array size doesn't match the ushort data type."); + bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16); } - Array.Reverse(bytes); - - return BitConverter.ToUInt16(bytes, 0); + return bytes; } - public static uint BEBytesToUInt32(byte[] bytes) + public static byte[] TrimArray(byte[] bytes) { - if (bytes.Length != sizeof(uint)) + int trimStart = bytes.Length - 1; + while (trimStart >= 0 && bytes[trimStart] == 0) { - throw new InteropException("Array size doesn't match the uint data type."); + trimStart--; } - Array.Reverse(bytes); + byte[] result = new byte[trimStart + 1]; + Array.Copy(bytes, 0, result, 0, trimStart + 1); - return BitConverter.ToUInt32(bytes, 0); + return result; } - public static ulong BEBytesToUInt64(byte[] bytes) + public static string ToHex(byte[] bytes) { - if (bytes.Length != sizeof(ulong)) + var sb = new StringBuilder(); + foreach (var b in bytes) { - throw new InteropException("Array size doesn't match the ulong data type."); + sb.AppendFormat("{0:x2}", b); } - - Array.Reverse(bytes); - - return BitConverter.ToUInt64(bytes, 0); - } - - public static IEnumerable 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 HexToList(string hex) - { - return HexToEnumerable(hex).ToList(); + return sb.ToString(); } - public static string ToHex(IEnumerable bytes) + public static uint GetTime() { - StringBuilder sb = new StringBuilder(); - foreach (byte b in bytes) - { - sb.AppendFormat("{0:x2}", b); - } - return sb.ToString(); + return (uint)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds; } } }