-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBaseConversion.cs
499 lines (410 loc) · 16.6 KB
/
BaseConversion.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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Text.RegularExpressions;
namespace PrimitiveTypes
{
[TestClass]
public class BaseConversion
{
[TestMethod]
[TestCategory("13_Convert_To_Base")]
[ExpectedException(typeof(ArgumentException))]
public void Convert_From_Base_10_To_A_Base_Lower_Than_2() {
byte[] result = ConvertToBase(9, 0);
}
[TestMethod]
[TestCategory("13_Convert_To_Base")]
public void Convert_From_Base_10_To_Base_2_No_9() {
int expected = 9;
int cBase = 2;
byte[] result = ConvertToBase(expected, cBase);
int actual = ConvertFromBase(result, cBase);
Assert.AreEqual(expected, actual);
}
[TestMethod]
[TestCategory("13_Convert_To_Base")]
public void Convert_From_Base_10_To_Base_2_No_1119() {
int expected = 1119;
int cBase = 2;
byte[] result = ConvertToBase(expected, cBase);
int actual = ConvertFromBase(result, cBase);
Assert.AreEqual(expected, actual);
}
[TestMethod]
[TestCategory("13_Convert_To_Base")]
public void Convert_From_Base_10_To_Base_2_No_172() {
int expected = 172;
int cBase = 2;
byte[] result = ConvertToBase(expected, cBase);
int actual = ConvertFromBase(result, cBase);
Assert.AreEqual(expected, actual);
}
[TestMethod]
[TestCategory("13_Convert_To_Base")]
public void Convert_From_Base_8_To_Base_10_No_172() {
int expected = 172;
int cBase = 8;
byte[] result = ConvertToBase(expected, cBase);
int actual = ConvertFromBase(result, cBase);
Assert.AreEqual(expected, actual);
}
[TestMethod]
[TestCategory("13_Convert_To_Base")]
public void Test_Bitwise_NOT() {
byte[] actual = ConvertToBase(4, 2);
byte[] expected = { 1, 1, 0, 1, 1, 1, 1, 1 };
NOT(actual);
CollectionAssert.AreEqual(expected, actual);
}
[TestMethod]
[TestCategory("13_Convert_To_Base")]
public void Test_Bitwise_AND_When_Params_Have_Same_Length() {
int expected = 4 & 4;
byte[] first = ConvertToBase(4, 2);
byte[] second = ConvertToBase(4, 2);
int actual = ConvertFromBase(AND(first, second), 2);
Assert.AreEqual(expected, actual);
}
[TestMethod]
[TestCategory("13_Convert_To_Base")]
public void Test_Bitwise_AND_When_Params_Have_Different_Lengths() {
int expected = 255 & 384;
byte[] first = ConvertToBase(255, 2);
byte[] second = ConvertToBase(384, 2);
int actual = ConvertFromBase(AND(first, second), 2);
Assert.AreEqual(expected, actual);
}
[TestMethod]
[TestCategory("13_Convert_To_Base")]
public void Test_Bitwise_AND_When_Params_Have_Different_Lengths2() {
int expected = 255 & 256;
byte[] first = ConvertToBase(255, 2);
byte[] second = ConvertToBase(256, 2);
int actual = ConvertFromBase(AND(first, second), 2);
Assert.AreEqual(expected, actual);
}
[TestMethod]
[TestCategory("13_Convert_To_Base")]
public void Test_Bitwise_OR_When_Params_Have_Same_Length() {
int expected = 6 | 8;
byte[] first = ConvertToBase(6, 2);
byte[] second = ConvertToBase(8, 2);
int actual = ConvertFromBase(OR(first, second), 2);
Assert.AreEqual(expected, actual);
}
[TestMethod]
[TestCategory("13_Convert_To_Base")]
public void Test_Bitwise_OR_When_Params_Have_Different_Lengths() {
int expected = 255 | 384;
byte[] first = ConvertToBase(255, 2);
byte[] second = ConvertToBase(384, 2);
int actual = ConvertFromBase(OR(first, second), 2);
Assert.AreEqual(expected, actual);
}
[TestMethod]
[TestCategory("13_Convert_To_Base")]
public void Test_Bitwise_XOR_When_Params_Have_Same_Length() {
int expected = 6 ^ 8;
byte[] first = ConvertToBase(6, 2);
byte[] second = ConvertToBase(8, 2);
int actual = ConvertFromBase(XOR(first, second), 2);
Assert.AreEqual(expected, actual);
}
[TestMethod]
[TestCategory("13_Convert_To_Base")]
public void Test_Bitwise_XOR_When_Params_Have_Different_Lengths() {
int expected = 255 ^ 384;
byte[] first = ConvertToBase(255, 2);
byte[] second = ConvertToBase(384, 2);
int actual = ConvertFromBase(XOR(first, second), 2);
Assert.AreEqual(expected, actual);
}
[TestMethod]
[TestCategory("13_Convert_To_Base")]
public void Test_Bitwise_Left_Shift() {
byte[] bytes = ConvertToBase(129, 2);
byte[] expected = ConvertToBase(129 << 17, 2);
byte[] actual = LeftShift(bytes, 17);
CollectionAssert.AreEqual(expected, actual);
}
[TestMethod]
[TestCategory("13_Convert_To_Base")]
public void Test_Bitwise_Right_Shift() {
byte[] bytes = ConvertToBase(65536, 2);
byte[] expected = ConvertToBase(65536 >> 9, 2);
byte[] actual = RightShift(bytes, 9);
CollectionAssert.AreEqual(expected, actual);
}
[TestMethod]
[TestCategory("13_Convert_To_Base")]
public void Test_128_LessThan_256() {
byte[] reference = ConvertToBase(128, 2);
byte[] compared = ConvertToBase(256, 2);
bool result = LessThan(reference, compared);
Assert.IsTrue(result);
}
[TestMethod]
[TestCategory("13_Convert_To_Base")]
public void Test_256_GraterThan_128() {
byte[] reference = ConvertToBase(256, 2);
byte[] compared = ConvertToBase(128, 2);
bool result = GraterThan(reference, compared);
Assert.IsTrue(result);
}
[TestMethod]
[TestCategory("13_Convert_To_Base")]
public void Test_256_EqualTo_256() {
byte[] reference = ConvertToBase(256, 2);
byte[] compared = ConvertToBase(256, 2);
bool result = EqualTo(reference, compared);
Assert.IsTrue(result);
}
[TestMethod]
[TestCategory("13_Convert_To_Base")]
public void Test_256_NotEqualTo_257() {
byte[] reference = ConvertToBase(257, 2);
byte[] compared = ConvertToBase(256, 2);
bool result = NotEqualTo(reference, compared);
Assert.IsTrue(result);
}
[TestMethod]
[TestCategory("13_Convert_To_Base")]
public void Test_Add_49_To_24_In_Base_5()
{
byte[] result = AddInBase(49, 24, 5);
int actual = ConvertFromBase(result, 5);
Assert.AreEqual(73, actual);
}
[TestMethod]
[TestCategory("13_Convert_To_Base")]
public void Test_Multiply_49_To_24_In_Base_5() {
byte[] result = MultiplyInBase(49, 24, 5);
int actual = ConvertFromBase(result, 5);
Assert.AreEqual(1176, actual);
}
[TestMethod]
[TestCategory("13_Convert_To_Base")]
public void Test_Multiply_49_To_0_In_Base_5() {
byte[] result = MultiplyInBase(49, 0, 5);
int actual = ConvertFromBase(result, 5);
Assert.AreEqual(0, actual);
}
[TestMethod]
[TestCategory("13_Convert_To_Base")]
public void Test_Substract_3_From_4_In_Base_5() {
byte[] result = SubstractInBase(4, 3, 5);
int actual = ConvertFromBase(result, 5);
Assert.AreEqual(1, actual);
}
[TestMethod]
[TestCategory("13_Convert_To_Base")]
public void Test_Substract_4_From_625_In_Base_5() {
byte[] result = SubstractInBase(625, 4, 5);
int actual = ConvertFromBase(result, 5);
Assert.AreEqual(621, actual);
}
[TestMethod]
[TestCategory("13_Convert_To_Base")]
public void Test_Devide_625_With_5_In_Base_5() {
byte[] result = DevideInBase(625, 5, 5);
int actual = ConvertFromBase(result, 5);
Assert.AreEqual(125, actual);
}
private byte[] DevideInBase(int firstValue, int secondValue, int inBase) {
ValidateBase(inBase);
byte[] result = new byte[0];
if (secondValue == 0) throw new ArgumentException("Atempt to devide by zero.");
int difference = firstValue;
int divisions = 0;
while(difference > 0) {
result = SubstractInBase(difference, secondValue, inBase);
difference = ConvertFromBase(result, inBase);
divisions++;
}
return ConvertToBase(divisions, inBase);
}
private byte[] SubstractInBase(int firstValue, int secondValue, int inBase) {
ValidateBase(inBase);
if (firstValue < secondValue) throw new ArgumentException("First argument needs to be bigger than second.");
byte[] first = ConvertToBase(firstValue, inBase);
byte[] second = ConvertToBase(secondValue, inBase);
byte[] result = new byte[first.Length];
int borrowed = 0;
for (int i = 0; i < first.Length; i++) {
if (first[i] - borrowed < second[i]) {
result[i] = (byte)(first[i] + inBase - borrowed - second[i]);
borrowed = 1;
} else {
result[i] = (byte)(first[i] - borrowed - second[i]);
borrowed = 0;
}
}
return result;
}
private byte[] MultiplyInBase(int firstValue, int secondValue, int inBase) {
ValidateBase(inBase);
byte[] result = new byte[0];
if (firstValue == 0 || secondValue == 0) return result;
int sum = 0;
for (int i = 0; i < secondValue; i++) {
result = AddInBase(sum, firstValue, inBase);
sum = ConvertFromBase(result, inBase);
}
return result;
}
private byte[] AddInBase(int firstValue, int secondValue, int inBase)
{
byte[] first = ConvertToBase(firstValue, inBase);
byte[] second = ConvertToBase(secondValue, inBase);
int minSize = Math.Min(first.Length, second.Length);
byte[] result = new byte[minSize];
int toCarry = 0;
for (int i = 0; i < minSize; i++) {
int sum = (byte)(first[i] + second[i]) + toCarry;
toCarry = 0;
if (sum >= inBase) {
sum = (byte)(sum % inBase);
toCarry++;
}
result[i] = (byte)sum;
}
return result;
}
private bool NotEqualTo(byte[] reference, byte[] compared) {
return (LessThan(reference, compared)) || GraterThan(reference, compared);
}
private bool EqualTo(byte[] reference, byte[] compared) {
return !(LessThan(reference, compared)) && !GraterThan(reference, compared);
}
private bool GraterThan(byte[] reference, byte[] compared) {
return LessThan(compared, reference);
}
private bool LessThan(byte[] reference, byte[] compared) {
int referenceIndex = GetStartIndex(reference);
int compareIndex = GetStartIndex(compared);
if (referenceIndex < compareIndex) return true;
if (referenceIndex == compareIndex) {
for (int i = 0; i < reference.Length; i++) {
if (reference[i] < compared[i]) return true;
}
return false;
}
return false;
}
private byte[] RightShift(byte[] bytes, int moves) {
if (moves == 0) return bytes;
byte[] result = bytes;
while (moves > 0) {
result = RightShiftOneBit(result);
moves--;
}
return result;
}
private byte[] RightShiftOneBit(byte[] bytes) {
int baseSize = 8;
byte[] result = new byte[bytes.Length];
int index = GetStartIndex(bytes);
if (index % baseSize == 0) Array.Resize(ref result, bytes.Length - baseSize);
Array.Copy(bytes, 1, result, 0, index);
return result;
}
private byte[] LeftShift(byte[] bytes, int moves) {
if (moves == 0) return bytes;
byte[] result = bytes;
while (moves > 0) {
result = LeftShiftOneBit(result);
moves--;
}
return result;
}
private byte[] LeftShiftOneBit(byte[] bytes) {
int lastIndex = bytes.Length - 1;
if (bytes[lastIndex] == 1) ResizeArray(ref bytes);
int index = GetStartIndex(bytes);
byte[] result = new byte[bytes.Length];
Array.Copy(bytes, 0, result, 1, index + 1);
return result;
}
private int GetStartIndex(byte[] bytes) {
int result = 0;
for (int i = bytes.Length - 1; i >= 0; i--) {
if (bytes[i] == 1) return i;
}
return result;
}
private byte[] XOR(byte[] first, byte[] second) {
int minSize = Math.Min(first.Length, second.Length);
byte[] result = GetLargerArray(first, second);
for (int i = 0; i < minSize; i++) {
result[i] = (byte)((first[i] + second[i] == 1) ? 1 : 0);
}
return result;
}
private byte[] OR(byte[] first, byte[] second) {
int minSize = Math.Min(first.Length, second.Length);
byte[] result = GetLargerArray(first, second);
for (int i = 0; i < minSize; i++) {
if (first[i] + second[i] > 0) result[i] = 1;
}
return result;
}
private byte[] AND(byte[] first, byte[] second) {
int maxSize = Math.Max(first.Length, second.Length);
int minSize = Math.Min(first.Length, second.Length);
byte[] result = new byte[maxSize];
for (int i = 0; i < minSize; i++) {
result[i] = (byte)(first[i] * second[i]);
}
return result;
}
private void NOT(byte[] bytes) {
for (int i = 0; i < bytes.Length; i++) {
bytes[i] = (byte)((bytes[i] == 0) ? 1 : 0);
}
}
/// <summary>
/// Converts a number from any base to base 10.
/// </summary>
/// <param name="value">The value to be converted.</param>
/// <param name="fromBase">From base.</param>
/// <returns></returns>
private int ConvertFromBase(byte[] value, int fromBase) {
int result = 0;
for(int i = 0; i < value.Length; i++) {
byte cellValue = value[i];
result += cellValue * (int)Math.Pow(fromBase, i);
}
return result;
}
/// <summary>
/// Converts a number from base 10 to any base.
/// </summary>
/// <param name="value">The value to be converted.</param>
/// <param name="toBase">To base.</param>
/// <returns></returns>
private byte[] ConvertToBase(int value, int toBase) {
ValidateBase(toBase);
byte[] bytes = new byte[8];
int divisionResult = value;
int remainder;
for(int i = 0; divisionResult > 0; i++) {
if (i == bytes.Length) bytes = ResizeArray(ref bytes);
remainder = divisionResult % toBase;
bytes[i] = (byte)remainder;
divisionResult = (divisionResult - remainder) / toBase;
}
return bytes;
}
private static byte[] GetLargerArray(byte[] first, byte[] second) {
return (first.Length >= second.Length) ? first: second;
}
private static byte[] ResizeArray(ref byte[] bytes) {
int baseSize = 8;
Array.Resize(ref bytes, bytes.Length + baseSize);
return bytes;
}
private static void ValidateBase(int toBase) {
if (toBase < 2) throw new ArgumentException("Invalid base.");
}
}
}