-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utils.cs
432 lines (393 loc) · 15.2 KB
/
Utils.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Plutarque
{
public class Utils
{
/// <summary>
/// Indique la base maximale qui pourra donner un résultata affichable
/// dans les fonctions telles que ToBaseString(...)
/// </summary>
/// <returns></returns>
public static int GetMaxSupportedBase()
{
return alph.Length;
}
public static string alph = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
public static StringFormat format = new StringFormat()
{
Alignment = StringAlignment.Center,
LineAlignment = StringAlignment.Center,
Trimming = StringTrimming.None,
FormatFlags = StringFormatFlags.NoWrap
};
public static Color GetForeColorFromBackColor(Color clr)
{
//Idée de Gacek : https://stackoverflow.com/questions/1855884/determine-font-color-based-on-background-color
double lum = (0.299 * clr.R + 0.587 * clr.G + 0.114 * clr.B) / 255;
if (lum > 0.5)
return Color.Black;
else
return Color.White;
}
public static Brush defBr = new SolidBrush(SystemColors.WindowText);
public static Brush selBr = new SolidBrush(SystemColors.HighlightText);
public static Brush selBackBr = new SolidBrush(SystemColors.Highlight);
public static Brush repBr = new SolidBrush(Color.Aqua);
public static HatchBrush delBr = new HatchBrush(HatchStyle.BackwardDiagonal, Color.Red, Color.Transparent);
/// <summary>
/// Retourne la longueur maximale de la représentation d'un octet en base b (le nombre de chiffres)
/// </summary>
/// <param name="b"></param>
/// <returns></returns>
public static int GetMaxLengthOfByte(int b)
{
return (int)Math.Ceiling(Math.Log(255, b));
}
public static string ToBaseString(double n, int b)
{
return n < 0 ? ToBaseString((long)n, b) : ToBaseString((ulong)n, b);
}
/// <summary>
/// Fournit la représentation sous forme de chaîne de caractères
/// de l'octet spécifié avec la base indiquée.
/// </summary>
/// <param name="n">Octet à convertir</param>
/// <param name="b">Base de numération, -1 pour ASCII</param>
/// <returns></returns>
public static string ToBaseString(byte n, int b)
{
if (b > 0)
{
int cur = Math.Abs(n);
int rem;
string ret = "";
while (cur > 0)
{
cur = Math.DivRem(cur, b, out rem);
ret = alph[rem] + ret;
}
if (n < 0)
ret = "-" + ret;
return ret.PadLeft(GetMaxLengthOfByte(b), '0');
}
else
{
//ascii latin1
return new string((char)Math.Max((byte)32, n), 1);
}
}
/// <summary>
/// Fournit la représentation sous forme de chaîne de caractères
/// de l'entier spécifié avec la base indiquée.
/// </summary>
/// <param name="n">Octet à convertir</param>
/// <param name="b">Base de numération, doit être au moins 1.</param>
/// <returns></returns>
public static string ToBaseString(long n, int b)
{
long cur = Math.Abs(n);
long rem = 0;
string ret = "";
while (cur > 0)
{
cur = Math.DivRem(cur, b, out rem);
ret = alph[(int)(rem % alph.Length)] + ret;
}
if (n < 0)
ret = "-" + ret;
return ret.PadLeft(GetMaxLengthOfByte(b) * GetNbOfBytes(n), '0');
}
/// <summary>
/// Retourne une chaîne dans la base de numération indiquée.
/// </summary>
/// <param name="b">Base, -1 pour l'ASCII</param>
/// <param name="array">Le tableau d'où tirer une représentation textuelle.</param>
/// <param name="lineLength">Longueur de la ligne, 0 pour une ligne non limitée.</param>
/// <returns></returns>
public static string ArrayToString(int b, byte[] array, int lineLength = 0)
{
string ret = "";
if (lineLength == 0)
foreach (byte item in array)
{
ret += ToBaseString(item, b) + (b > 0 ? " " : "");
}
else
for (int i = 0; i < array.Length; i += lineLength)
{
for (int j = i; j < array.Length && j < i + lineLength; j++)
{
byte item = array[i];
ret += ToBaseString(item, b) + (b > 0 ? " " : "");
}
ret += "\r\n";
}
return ret;
}
/// <summary>
/// Fournit la représentation sous forme de chaîne de caractères
/// de l'entier spécifié avec la base indiquée.
/// </summary>
/// <param name="n">Octet à convertir</param>
/// <param name="b">Base de numération</param>
/// <returns></returns>
public static string ToBaseString(ulong n, int b)
{
ulong cur = n;
ulong rem = 0;
string ret = "";
while (cur > 0)
{
cur = DivRem(cur, (ulong)b, out rem);
ret = alph[((int)rem % alph.Length)] + ret;
}
if (n < 0)
ret = "-" + ret;
return ret.PadLeft(GetMaxLengthOfByte(b) * GetNbOfBytes(n), '0');
}
public static ulong DivRem(ulong a, ulong b, out ulong result)
{
result = a % b;
return a / b;
}
public static int GetNbOfBytes(long n)
{
return n == 0 ? 1 : (int)Math.Log(n, 256.0);
}
public static int GetNbOfBytes(double n)
{
return n == 0 ? 1 : (int)Math.Log(n, 256.0);
}
/*public static double ToBigNum(object o)
{
double d = 0;
if (double.TryParse(o.ToString(), out d))
{
return d;
}
else
return 0;
}*/
/// <summary>
/// Convertit la chaîne de caractères spécifiée (UTF8) en bloc d'octets.
/// </summary>
/// <param name="str">Chaîne de caractères encodée en UTF8</param>
/// <returns>Bloc d'octets correspondant</returns>
public static byte[] StrToBytes(string str)
{
if (str == null)
{
byte[] b = new byte[0];
return b;
}
else
return System.Text.Encoding.UTF8.GetBytes(str);
}
/// <summary>
/// Retourne la chaîne de caractères UTF8 correspondant au bloc d'octets spécifié.
/// </summary>
/// <param name="data">Bloc d'octets encodant une chaîne de caractères UTF8</param>
/// <returns>Chaîne de caractères correspondante</returns>
public static string BytesToStr(byte[] data)
{
return new string(System.Text.Encoding.UTF8.GetChars(data));
}
/// <summary>
/// Convertit la chaîne de caractères spécifiée (Unicode) en bloc d'octets.
/// </summary>
/// <param name="str">Chaîne de caractères encodée en Unicode</param>
/// <returns>Bloc d'octets correspondant</returns>
public static byte[] UniStrToBytes(string str)
{
if (str == null)
{
byte[] b = new byte[0];
return b;
}
else
return System.Text.Encoding.Unicode.GetBytes(str);
}
/// <summary>
/// Retourne la chaîne de caractères Unicode correspondant au bloc d'octets spécifié.
/// </summary>
/// <param name="data">Bloc d'octets encodant une chaîne de caractères Unicode</param>
/// <returns>Chaîne de caractères correspondante</returns>
public static string BytesToUniStr(byte[] data)
{
return new string(System.Text.Encoding.Unicode.GetChars(data));
}
/// <summary>
/// Convertit un entier 64 bits non signé en bloc d'octets
/// </summary>
/// <param name="nbr">Entier 64 bits</param>
/// <returns>Bloc d'octets</returns>
public static byte[] ULongToBytes(ulong nbr)
{
return BitConverter.GetBytes(nbr);
}
/// <summary>
/// Convertit un entier 32 bits non signé en bloc d'octets
/// </summary>
/// <param name="nbr">Entier 32 bits</param>
/// <returns>Bloc d'octets</returns>
public static byte[] UInt32ToBytes(uint nbr)
{
return BitConverter.GetBytes(nbr);
}
/// <summary>
/// Convertit un entier 32 bits signé en bloc d'octets
/// </summary>
/// <param name="nbr">Entier 32 bits</param>
/// <returns>Bloc d'octets</returns>
public static byte[] IntToBytes(int nbr)
{
return BitConverter.GetBytes(nbr);
}
/// <summary>
/// Convertit une couleur en un bloc d'octet équivalent à la valeur ARGB 32 bits.
/// </summary>
/// <param name="clr">Couleur à convertir</param>
/// <returns>Bloc d'octets</returns>
public static byte[] ColorToBytes(Color clr)
{
return BitConverter.GetBytes(clr.ToArgb());
}
/// <summary>
/// Convertit un entier 16 bits non signé en bloc d'octets
/// </summary>
/// <param name="nbr">Entier 16 bits</param>
/// <returns>Bloc d'octets</returns>
public static byte[] UShortToBytes(ushort nbr)
{
return BitConverter.GetBytes(nbr);
}
/// <summary>
/// Convertit un entier 64 bits signé en bloc d'octets
/// </summary>
/// <param name="nbr">Entier 64 bits</param>
/// <returns>Bloc d'octets</returns>
public static byte[] LongToBytes(long nbr)
{
return BitConverter.GetBytes(nbr);
}
/// <summary>
/// Convertit une date/heure en bloc d'octets
/// </summary>
/// <param name="nbr">Objet DateTime à convertir</param>
/// <returns>Bloc d'octets</returns>
public static byte[] DateToBytes(DateTime dT)
{
return LongToBytes(dT.ToBinary());
}
/// <summary>
/// Convertit le bloc d'octet spécifié en date/heure
/// </summary>
/// <param name="nbr">Bloc d'octets (64 bits)</param>
/// <returns>Objet DateTime correspondant</returns>
public static DateTime BytesToDate(byte[] b)
{
return DateTime.FromBinary(BytesToLong(b));
}
/// <summary>
/// Modes d'édition dans un RTFControl
/// </summary>
public enum TextMode : byte
{
RawText = 0x10,
RTF = 0x11,
HTML = 0x12
}
/// <summary>
/// Retourne l'octet correspondant à la verion spécifiée.
/// </summary>
/// <param name="majeure">Composante majeure</param>
/// <param name="mineure">Composante mineure</param>
/// <returns>Octet de version</returns>
/// <remarks>Notez que la composante mineure comporte le poids le plus faible.</remarks>
public static byte GetVersion(byte majeure, byte mineure)
{
return (byte)(majeure * (byte)0x10 + mineure);
}
/// <summary>
/// Convertit le bloc d'octets spécifié en entier 32 bits non signé.
/// </summary>
/// <param name="data">Bloc d'octets à convertir</param>
/// <returns>Entier 32 bits non signé</returns>
public static uint BytesToUInteger(byte[] data)
{
return BitConverter.ToUInt32(data, 0);
}
/// <summary>
/// Convertit le bloc d'octets spécifié en entier 32 bits signé.
/// </summary>
/// <param name="data">Bloc d'octets à convertir</param>
/// <returns>Entier 32 bits signé</returns>
public static int BytesToInteger(byte[] data)
{
return BitConverter.ToInt32(data, 0);
}
/// <summary>
/// Convertit le bloc d'octets spécifié en couleur.
/// </summary>
/// <param name="data">Bloc d'octets à convertir</param>
/// <returns>Couleur correspondante</returns>
public static Color BytesToColor(byte[] data)
{
return Color.FromArgb(BitConverter.ToInt32(data, 0));
}
/// <summary>
/// Convertit le bloc d'octets spécifié en entier 64 bits non signé.
/// </summary>
/// <param name="data">Bloc d'octets à convertir</param>
/// <returns>Entier 64 bits non signé</returns>
public static ulong BytesToULong(byte[] data)
{
return BitConverter.ToUInt64(data, 0);
}
/// <summary>
/// Convertit le bloc d'octets spécifié en entier 16 bits non signé.
/// </summary>
/// <param name="data">Bloc d'octets à convertir</param>
/// <returns>Entier 16 bits non signé</returns>
public static ushort BytesToUShort(byte[] data)
{
return BitConverter.ToUInt16(data, 0);
}
/// <summary>
/// Convertit le bloc d'octets spécifié en entier 16 bits signé.
/// </summary>
/// <param name="data">Bloc d'octets à convertir</param>
/// <returns>Entier 16 bits signé</returns>
public static short BytesToShort(byte[] data)
{
return BitConverter.ToInt16(data, 0);
}
/// <summary>
/// Convertit le bloc d'octets spécifié en entier 64 bits signé.
/// </summary>
/// <param name="data">Bloc d'octets à convertir</param>
/// <returns>Entier 64 bits signé</returns>
public static long BytesToLong(byte[] data)
{
return BitConverter.ToInt64(data, 0);
}
/// <summary>
/// Retourne la date/heure correspondant au bloc d'octets spécifié.
/// </summary>
/// <param name="data">Bloc d'octets (64 bits) à convertir</param>
/// <returns>Objet DateTime</returns>
public static DateTime DateFromBytes(byte[] data)
{
return DateTime.FromBinary(BytesToLong(data));
}
}
}