TrimArray() simplification
[NovacoinLibrary.git] / Novacoin / Interop.cs
1 \feff/**
2  *  Novacoin classes library
3  *  Copyright (C) 2015 Alex D. (balthazar.ad@gmail.com)
4
5  *  This program is free software: you can redistribute it and/or modify
6  *  it under the terms of the GNU Affero General Public License as
7  *  published by the Free Software Foundation, either version 3 of the
8  *  License, or (at your option) any later version.
9
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU Affero General Public License for more details.
14
15  *  You should have received a copy of the GNU Affero General Public License
16  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 using System;
20 using System.Collections.Generic;
21 using System.Linq;
22 using System.Text;
23
24 namespace Novacoin
25 {
26     /// <summary>
27     /// Miscellaneous functions
28     /// </summary>
29     public class Interop
30     {
31         /// <summary>
32         /// Convert array of unsigned integers to array of bytes.
33         /// </summary>
34         /// <param name="values">Array of unsigned integer values.</param>
35         /// <returns>Byte array</returns>
36         public static byte[] LEBytes(uint[] values)
37         {
38             var result = new byte[values.Length * sizeof(uint)];
39             Buffer.BlockCopy(values, 0, result, 0, result.Length);
40
41             return result;
42         }
43
44         /// <summary>
45         /// Convert byte array to array of unsigned integers.
46         /// </summary>
47         /// <param name="bytes">Byte array.</param>
48         /// <returns>Array of integers</returns>
49         public static uint[] ToUInt32Array(byte[] bytes)
50         {
51             var result = new uint[bytes.Length / sizeof(uint)];
52             Buffer.BlockCopy(bytes, 0, result, 0, bytes.Length);
53
54             return result;
55         }
56
57         /// <summary>
58         /// Reverse byte array
59         /// </summary>
60         /// <param name="source">Source array</param>
61         /// <returns>Result array</returns>
62         public static byte[] ReverseBytes(byte[] source)
63         {
64             var b = new byte[source.Length];
65
66             source.CopyTo(b, 0);
67
68             Array.Reverse(b);
69
70             return b;
71         }
72
73         public static byte[] HexToArray(string hex)
74         {
75             int nChars = hex.Length;
76             var bytes = new byte[nChars / 2];
77
78             for (int i = 0; i < nChars; i += 2)
79             {
80                 bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
81             }
82
83             return bytes;
84         }
85
86         public static byte[] TrimArray(byte[] bytes)
87         {
88             int trimStart = bytes.Length - 1;
89             while (trimStart >= 0 && bytes[trimStart] == 0)
90             {
91                 trimStart--;
92             }
93
94             return bytes.Take(trimStart + 1).ToArray();
95         }
96
97         public static byte[] AppendWithZeros(byte[] bytes, int nTargetLen=32)
98         {
99             var result = new byte[nTargetLen];
100             bytes.CopyTo(result, 0);
101
102             return result;
103         }
104
105         public static string ToHex(byte[] bytes)
106         {
107             var sb = new StringBuilder();
108             foreach (var b in bytes)
109             {
110                 sb.AppendFormat("{0:x2}", b);
111             }
112             return sb.ToString();
113         }
114
115         public static uint GetTime()
116         {
117             return (uint)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds;
118         }
119     }
120 }