We're at little-endian only
[NovacoinLibrary.git] / Novacoin / Interop.cs
1 \feffusing System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace Novacoin
7 {
8     public class InteropException : Exception
9     {
10         public InteropException()
11         {
12         }
13
14         public InteropException(string message)
15             : base(message)
16         {
17         }
18
19         public InteropException(string message, Exception inner)
20             : base(message, inner)
21         {
22         }
23     }
24
25     public class Interop
26     {
27         public static byte[] ReverseBytes(byte[] source)
28         {
29             Array.Reverse(source);
30
31             return source;
32         }
33
34         public static byte[] LEBytes(uint[] values)
35         {
36             byte[] result = new byte[values.Length * sizeof(uint)];
37             Buffer.BlockCopy(values, 0, result, 0, result.Length);
38
39             return result;
40         }
41
42         public static uint[] ToUInt32Array(byte[] bytes)
43         {
44             uint[] result = new uint[bytes.Length / sizeof(uint)];
45             Buffer.BlockCopy(bytes, 0, result, 0, bytes.Length);
46
47             return result;
48         }
49
50         public static byte[] BEBytes(ushort n)
51         {
52             byte[] resultBytes = BitConverter.GetBytes(n);
53
54             Array.Reverse(resultBytes);
55
56             return resultBytes;
57         }
58
59         public static byte[] BEBytes(uint n)
60         {
61             byte[] resultBytes = BitConverter.GetBytes(n);
62
63             Array.Reverse(resultBytes);
64
65             return resultBytes;
66         }
67
68         public static byte[] BEBytes(ulong n)
69         {
70             byte[] resultBytes = BitConverter.GetBytes(n);
71
72             Array.Reverse(resultBytes);
73
74             return resultBytes;
75         }
76
77
78         public static ushort BEBytesToUInt16(byte[] bytes)
79         {
80             if (bytes.Length != sizeof(ushort))
81             {
82                 throw new InteropException("Array size doesn't match the ushort data type.");
83             }
84
85             Array.Reverse(bytes);
86
87             return BitConverter.ToUInt16(bytes, 0);
88         }
89
90         public static uint BEBytesToUInt32(byte[] bytes)
91         {
92             if (bytes.Length != sizeof(uint))
93             {
94                 throw new InteropException("Array size doesn't match the uint data type.");
95             }
96
97             Array.Reverse(bytes);
98
99             return BitConverter.ToUInt32(bytes, 0);
100         }
101
102         public static ulong BEBytesToUInt64(byte[] bytes)
103         {
104             if (bytes.Length != sizeof(ulong))
105             {
106                 throw new InteropException("Array size doesn't match the ulong data type.");
107             }
108
109             Array.Reverse(bytes);
110
111             return BitConverter.ToUInt64(bytes, 0);
112         }
113
114         public static IEnumerable<byte> ParseHex(string hex)
115         {
116             return Enumerable.Range(0, hex.Length)
117                              .Where(x => x % 2 == 0)
118                              .Select(x => Convert.ToByte(hex.Substring(x, 2), 16));
119         }
120
121         public static string ToHex(IEnumerable<byte> bytes)
122         {
123             StringBuilder sb = new StringBuilder();
124             foreach (byte b in bytes)
125             {
126                 sb.AppendFormat("{0:x2}", b);
127             }
128             return sb.ToString();
129         }
130     }
131 }