-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConversion.cs
751 lines (547 loc) · 23.2 KB
/
Conversion.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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Numerics;
using MaterialSkin;
using MaterialSkin.Controls;
namespace Convertor_Decimal
{
public partial class Conversion : Form
{
//readonly MaterialSkin.MaterialSkinManager materialSkinManager;
public Conversion()
{
InitializeComponent();
//materialSkinManager = MaterialSkinManager.Instance;
//materialSkinManager.AddFormToManage(this);
//materialSkinManager.Theme = MaterialSkinManager.Themes.LIGHT;
//materialSkinManager.ColorScheme = new ColorScheme(Primary.BlueGrey800, Primary.BlueGrey900, Primary.BlueGrey500, Accent.LightBlue200, TextShade.WHITE);
}
//******************** BACKEND CODE OF MY CONVERTER *************************************************//
// Power function
public BigInteger power(BigInteger base_value, BigInteger value )
{
if (value == 0) return 1;
return base_value * power(base_value, value - 1);
}
/*************************BINARY NUMBER SECTION **********************************/
public char check_char(BigInteger num)
{
char a = 'A';
BigInteger chek = 10;
for (int i = 0; i < 6; i++, chek++)
{
if (chek == num)
{
return a;
}
a++;
}
return a;
}
// For converting decimal to octal and hexa decimal
public string convert_decimal_octal_hexa(BigInteger dec, BigInteger divider)
{
string res = "";
if (divider == 8)
{
while (dec != 0) // Loop for dividng base 10 by base 8
{
res = Convert.ToString(dec % divider) + res;
dec /= divider; // divide by divider passed as 8
}
return res; // in case of 8
}
while (dec != 0) // Loop for dividing base 10 by base 16
{
BigInteger rem = dec % divider;
if (rem >= 10 && rem <= 15)
res = check_char(rem) + res;
else
res = Convert.ToString(dec % divider) + res;
dec /= divider; // divide by 16
}
return res;
}
public BigInteger convert_Binary_To_Decimal(string Binary)
{
BigInteger res = 0, m = 0; // Here m is used for indexing from 0 to length, i is starting from last of string
int i;
// It is converting binary to decimal notation
for (i = Binary.Length - 1; i >= 0; i--, m++)
{
if (Binary[i] == '1')
{
res += power(2, m); // here m is used as 1 as in 2^1.
}
}
return res;
}
// Used to convert Binary to other notations
public string Convert_Binary(string Binary, BigInteger base_Value)
{
BigInteger res = convert_Binary_To_Decimal(Binary);
if (base_Value == 8 && res > 7)
{
// Doing conversion from BigInteger to string to octal
return convert_decimal_octal_hexa(res, 8);
}
// Doing conversion from BigInteger to string to hexadecimal
else if (base_Value == 16 && res > 9)
{
return convert_decimal_octal_hexa(res, 16);
}
return res.ToString();
}
/*************************END OF BINARY NUMBER SECTION **********************************/
/*************************DECIMAL NUMBER SECTION **********************************/
public string Convert_Decimal(string Decimal,BigInteger base_value)
{
string res = "";
if (base_value == 2) // To convert decimal to binary
{
res = Decimal_To_Binary(BigInteger.Parse(Decimal));
}
else if (base_value == 8) // To convert decimal to octal
{
res = convert_decimal_octal_hexa(BigInteger.Parse(Decimal), 8);
}
else // To convert decimal to hexa decimal
res = convert_decimal_octal_hexa(BigInteger.Parse(Decimal), 16);
return res;
}
// Function used to convert decimal to binary
public string Decimal_To_Binary(BigInteger dec)
{
string bin = "";
while (dec != 0)
{ // Used to convert dec result in character like 0 and 1
bin = (dec % 2).ToString() + bin;
dec /= 2;
}
return bin;
}
/*************************END OF DECIMAL NUMBER SECTION **********************************/
/************************* OCTAL NUMBER SECTION **********************************/
public string Convert_Octal(string oct, BigInteger base_value)
{
string res = "";
// Convert octal to decimal then decimal to binary.
if (base_value == 2)
{
res = Decimal_To_Binary(convert_Octal_To_Decimal(oct));
}
// Convert octal to decimal
else if (base_value == 10)
{
res = convert_Octal_To_Decimal(oct).ToString();
}
else // Convert octal to decimal and then to hexadecimal
res = convert_decimal_octal_hexa(convert_Octal_To_Decimal(oct), 16);
return res;
}
public BigInteger convert_Octal_To_Decimal(string Oct)
{
BigInteger res = 0, m = 0; // Here m is used for indexing from 0 to length, i is starting from last of string
int i; // i is used for indexing of characters
// It is converting binary to decimal notation
for (i = Oct.Length - 1; i >= 0; i--, m++)
{
// 1 * 8^1 + 7 * 8^0 = 8 + 7 = 15 converting 17 to 15 decimal
res += (Oct[i]-'0' ) * power(8, m); // here m is used as 1 as in 2^1.
}
return res;
}
/*************************END OF OCTAL NUMBER SECTION **********************************/
/************************* HEXADECIMAL NUMBER SECTION **********************************/
// convert hexadecimal to decimal
public BigInteger check_char_hexa(char a)
{
BigInteger num = 10;
char check = 'A';
for (int i = 0; i < 6; i++, num++, check++)
{
if (check == a)
{
return num;
}
}
return -1;
}
public BigInteger Convert_Hexa_To_Decimal(string hexa)
{
BigInteger res = 0, m = 0; // Here m is used for indexing from 0 to length, i is starting from last of string
int i; // i is used for indexing of characters
// It is converting binary to decimal notation
for (i = hexa.Length - 1; i >= 0; i--, m++)
{
if (hexa[i]>='A' && hexa[i] <= 'F')
{
res += check_char_hexa(hexa[i]) * power(16, m);
}
else
res += (hexa[i] - '0') * power(16, m); // here m is used as 1 as in 2^1.
}
return res;
}
public string Convert_HexaDecimal(string hexa, BigInteger base_value)
{
string res = "";
// Convert hexadecimal to decimal to binary
if(base_value == 2)
{
res = Decimal_To_Binary(Convert_Hexa_To_Decimal(hexa));
}
else if (base_value == 10)
{
res = Convert_Hexa_To_Decimal(hexa).ToString();
}
else
res = convert_decimal_octal_hexa(convert_Octal_To_Decimal(hexa), 8);
return res;
}
/****************************END OF HEXA SECTION ************************************************/
// Reset the all tools of form of conversion
private void Reset_Click(object sender, EventArgs e)
{
m = 0;
hexr.Visible = octr.Visible = binr.Visible = decr.Visible= false;
hexr.Text = "hex";
octr.Text = "oct";
decr.Text = "dec";
binr.Text = "bin";
length_out.Visible = false;
ComboBox_List.SelectedIndex = 0; // Making default value of combo box to binary
InputBox.Clear();
for (int i = 0; i < Check_List.Items.Count; i++)
{
Check_List.SetItemChecked(i, false);
}
//Giving default values to textboxes of outputs
BinaryBox.Text = OctalBox.Text = DecimalBox.Text = HexaBox.Text = "Output here";
BinaryBox.Visible = OctalBox.Visible = DecimalBox.Visible = HexaBox.Visible = // text boxes
bin.Visible = oct.Visible = dec.Visible = hex.Visible = false; // labels
info.Text = "info";
info.Visible = false;
InputBox.Focus();
}
// Combox_List
private void InputBox_KeyPress(object sender, KeyPressEventArgs e, int m)
{
if (e.KeyChar == ' ')
e.Handled = true;
switch (m)
{
case 0:
if (!(Char.IsControl(e.KeyChar) || e.KeyChar == '0' || e.KeyChar == '1'))
e.Handled = true;
break;
case 1:
if (!(Char.IsControl(e.KeyChar) || (Char.IsNumber(e.KeyChar) && (int)e.KeyChar >= 48 && (int)e.KeyChar <= 55)))
e.Handled = true;
break;
case 2:
if (!(Char.IsControl(e.KeyChar) || Char.IsNumber(e.KeyChar)))
e.Handled = true;
break;
case 3:
if (!(Char.IsControl(e.KeyChar) || Char.IsNumber(e.KeyChar) || (e.KeyChar >= 'A' && e.KeyChar <= 'F')))
e.Handled = true;
break;
default:
break;
}
}
public int m;
// variable used for choice in key press args
private void Conversion_Load(object sender, EventArgs e)
{
ComboBox_List.SelectedIndex = 0; // Making default value of combo box to binary
Convert_Button.Size = new Size (96, 36);
Reset.Size = new Size (96, 36);
}
// TO handle keys related to specific notations like Binary for 0,1,Decimal 0-9, Octal 0-7, Hexa( 0-9,A-F)
private void InputBox_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == ' ')
e.Handled = true;
switch (m)
{
case 0:
if (!(Char.IsControl(e.KeyChar) || e.KeyChar == '0' || e.KeyChar == '1'))
e.Handled = true;
break;
case 1:
if (!(Char.IsControl(e.KeyChar) || (Char.IsNumber(e.KeyChar) && (int)e.KeyChar >= 48 && (int)e.KeyChar <= 55)))
e.Handled = true;
break;
case 2:
if (!(Char.IsControl(e.KeyChar) || Char.IsNumber(e.KeyChar)))
e.Handled = true;
break;
case 3:
if (!(Char.IsControl(e.KeyChar) || Char.IsNumber(e.KeyChar) || (e.KeyChar >= 'A' && e.KeyChar <= 'F')))
e.Handled = true;
break;
default:
break;
}
}
private void ComboBox_List_MouseDoubleClick(object sender, MouseEventArgs e)
{
InputBox.Clear();
}
private void InputBox_MouseHover(object sender, EventArgs e)
{
InputBox.Focus();
}
private void ComboBox_List_SelectedIndexChanged_1(object sender, EventArgs e)
{
InputBox.Clear();
string item = "";
if (ComboBox_List.SelectedItem != null)
item = ComboBox_List.SelectedItem.ToString();
else
{
MessageBox.Show("Please select any item from box where FROM is written !!!"); return;
}
if (item == "Binary")
{
m = 0;
InputBox_KeyPress(this, new KeyPressEventArgs((char)'\0'));
}
else if (item == "Octal")
{
m = 1;
InputBox_KeyPress(this, new KeyPressEventArgs((char)'\0'));
}
else if (item == "Decimal")
{
m = 2;
InputBox_KeyPress(this, new KeyPressEventArgs((char)'\0'));
}
else
{
m = 3;
InputBox_KeyPress(this, new KeyPressEventArgs((char)'\0'));
}
InputBox.Focus();
}
private void Convert_Button_Click(object sender, EventArgs e)
{
if (Check_List.SelectedItem == null) { // in case check list items are unselected
MessageBox.Show("Please Check Any item from box.\nBox is under location where TO is written");
return; }
if(InputBox.Text==null) // in case no number is entered by user in input box
{
MessageBox.Show("You have not entered any number.\nPlease fill the text box in order to use it !!!! ");
return;
}
BinaryBox.Text = OctalBox.Text = DecimalBox.Text = HexaBox.Text="Output here";
BinaryBox.Visible = OctalBox.Visible = DecimalBox.Visible = HexaBox.Visible = // text boxes
bin.Visible = oct.Visible = dec.Visible = hex.Visible // labels
= hexr.Visible =binr.Visible = octr.Visible = decr.Visible= false; // Lengths of outputs
info.Visible = true;
info.Text = "You entered "+InputBox.TextLength.ToString()+" characters in input";
// BINARY CASE
if(ComboBox_List.SelectedItem.ToString() == "Binary") {
if(Check_List.GetItemChecked(0)) // 2 by 2
{
BinaryBox.Visible = true;
bin.Visible = true;
BinaryBox.Text = InputBox.Text +" same result due to same types of conversions";
// Length
binr.Visible = true;
binr.Text = BinaryBox.Text.Length.ToString();
}
if(Check_List.GetItemChecked(1)) // 2 by 8
{
OctalBox.Visible = true;
oct.Visible = true;
OctalBox.Text = Convert_Binary(InputBox.Text , 8 ) ;
// Length
octr.Visible = true;
octr.Text = OctalBox.Text.Length.ToString();
}
if(Check_List.GetItemChecked(2)) // 2 by 10
{
DecimalBox.Visible = true;
dec.Visible = true;
DecimalBox.Text = Convert_Binary(InputBox.Text , 10 ) ;
// Length
decr.Visible = true;
decr.Text = DecimalBox.Text.Length.ToString();
}
if(Check_List.GetItemChecked(3)) // 2 by 16
{
HexaBox.Visible = true;
hex.Visible = true;
HexaBox.Text = Convert_Binary(InputBox.Text , 16 ) ;
// Length
hexr.Visible = true;
hexr.Text = HexaBox.Text.Length.ToString();
}
}
// OCTAL CASE
if (ComboBox_List.SelectedItem.ToString() == "Octal")
{
if (Check_List.GetItemChecked(0)) // 8 by 2
{
BinaryBox.Visible = true;
bin.Visible = true;
BinaryBox.Text = Convert_Octal(InputBox.Text,2);
// Length
binr.Visible = true;
binr.Text = BinaryBox.Text.Length.ToString();
}
if (Check_List.GetItemChecked(1)) // 8 by 8
{
OctalBox.Visible = true;
oct.Visible = true;
OctalBox.Text = InputBox.Text + " same result due to same types of conversions";
octr.Visible = true;
octr.Text = OctalBox.Text.Length.ToString();
}
if (Check_List.GetItemChecked(2)) // 8 by 10
{
DecimalBox.Visible = true;
dec.Visible = true;
DecimalBox.Text = Convert_Octal(InputBox.Text, 10);
decr.Visible = true;
decr.Text = DecimalBox.Text.Length.ToString();
}
if (Check_List.GetItemChecked(3)) // 8 by 16
{
HexaBox.Visible = true;
hex.Visible = true;
HexaBox.Text = Convert_Octal(InputBox.Text, 16);
hexr.Visible = true;
hexr.Text = HexaBox.Text.Length.ToString();
}
}
// Decimal Case
if (ComboBox_List.SelectedItem.ToString() == "Decimal")
{
if (Check_List.GetItemChecked(0)) // 10 by 2
{
BinaryBox.Visible = true;
bin.Visible = true;
BinaryBox.Text = Convert_Decimal(InputBox.Text, 2);
// Length
binr.Visible = true;
binr.Text = BinaryBox.Text.Length.ToString();
}
if (Check_List.GetItemChecked(1)) // 10 by 8
{
OctalBox.Visible = true;
oct.Visible = true;
OctalBox.Text = Convert_Decimal(InputBox.Text, 8);
octr.Visible = true;
octr.Text = OctalBox.Text.Length.ToString();
}
if (Check_List.GetItemChecked(2)) // 10 by 10
{
DecimalBox.Visible = true;
dec.Visible = true;
DecimalBox.Text = InputBox.Text + " same result due to same types of conversions";
decr.Visible = true;
decr.Text = DecimalBox.Text.Length.ToString();
}
if (Check_List.GetItemChecked(3)) // 10 by 16
{
HexaBox.Visible = true;
hex.Visible = true;
HexaBox.Text = Convert_Decimal(InputBox.Text, 16);
// Length of outputs
hexr.Visible = true;
hexr.Text = HexaBox.Text.Length.ToString();
}
}
// Hexa Decimal Case
if (ComboBox_List.SelectedItem.ToString() == "HexaDecimal")
{
if (Check_List.GetItemChecked(0)) // 16 by 2
{
BinaryBox.Visible = true;
bin.Visible = true;
BinaryBox.Text = Convert_HexaDecimal(InputBox.Text, 2);
// Length
binr.Visible = true;
binr.Text = BinaryBox.Text.Length.ToString();
}
if (Check_List.GetItemChecked(1)) // 16 by 8
{
OctalBox.Visible = true;
oct.Visible = true;
OctalBox.Text = Convert_HexaDecimal(InputBox.Text, 8);
octr.Visible = true;
octr.Text = OctalBox.Text.Length.ToString();
}
if (Check_List.GetItemChecked(2)) // 16 by 10
{
DecimalBox.Visible = true;
dec.Visible = true;
DecimalBox.Text = Convert_HexaDecimal(InputBox.Text, 10);
// Length
decr.Visible = true;
decr.Text = DecimalBox.Text.Length.ToString();
}
if (Check_List.GetItemChecked(3)) // 16 by 16
{
HexaBox.Visible = true;
hex.Visible = true;
HexaBox.Text = InputBox.Text + " same result due to same types of conversions";
//Length
hexr.Visible = true;
hexr.Text = HexaBox.Text.Length.ToString();
}
}
length_out.Visible = true;
}
//private void textBox1_KeyPress(object sender, KeyPressEventArgs e) // This is used to accept only Binary Number
//{
// if (!char.IsControl(e.KeyChar) && !(e.KeyChar == '0' || e.KeyChar == '1'))
// {
// e.Handled = true;
// // In case no control key or 0 or 1 then it will not accept these keys like a,b,c or 9,8
// }
//}
}
}
//private void InputBox_Click(object sender, EventArgs e)
//{
// InputBox.Clear();
// string item = "";
// if (ComboBox_List.SelectedItem != null)
// item = ComboBox_List.SelectedItem.ToString();
// else
// {
// MessageBox.Show("Please select any item from box where FROM is written !!!"); return;
// }
// if (item == "Binary")
// {
// m = 0;
// InputBox_KeyPress(this, new KeyPressEventArgs((char)'\0'));
// }
// else if (item == "Octal")
// {
// m = 1;
// InputBox_KeyPress(this, new KeyPressEventArgs((char)'\0'));
// }
// else if (item == "Decimal")
// {
// m = 2;
// InputBox_KeyPress(this, new KeyPressEventArgs((char)'\0'));
// }
// else
// {
// m = 3;
// InputBox_KeyPress(this, new KeyPressEventArgs((char)'\0'));
// }
//}