You are on page 1of 19

1

2 package SistemaNumerico;
3 import java.util.Scanner;
4
5 public class SistemaNumerico {
6
7 Scanner input = new Scanner(System.in);
8
9 /**
10 * This method convert the number from the Binary to the Decimal.
11 */
12 public void BinToDec() //B2D
13 {
14 System.out.println("Enter Number in Binary System: ");
15 String bin = input.next();
16 int length = bin.length();
17 int total = 0;
18 for (int i = length - 1, j = 0; i >= 0; i--, j++) {
19 int dec = 0;
20 if (!(bin.charAt(i) == '0' || bin.charAt(i) == '1')) {
21 System.out.println("Invalid value! ");
22 break;
23 } else {
24 switch (bin.charAt(i)) {
25 case '1':
26 dec = (int) Math.pow(2, j);
27 break;
28 case '0':
29 dec = 0;
30 break;
31
32 }
33 total += dec;
34 if (i == 0) {
35 System.out.println("Decimal System: " + total);
36 }
37 }
38 }
39
40 }
41
42 /**
43 * This method convert the number from the Octal to the Decimal.
44 */
45 public void OctToDec() //O2D
46 {
47 System.out.println("Enter Number in Octal System:");
48 String OctNumber = input.next();
49
50 int total = 0;
45 public void OctToDec() //O2D
46 {
47 System.out.println("Enter Number in Octal System:");
48 String OctNumber = input.next();
49
50 int total = 0;
51
52 String oct = OctNumber;
53 int length = oct.length();
54 boolean value = true;
55
56 for (int i = length - 1, j = 0; i >= 0; i--, j++) {
57 int dec = 0;
58
59 switch (oct.charAt(i)) {
60 case '0':
61 dec = 0;
62 break;
63 case '1':
64 dec = (int) Math.pow(8, j);
65 break;
66 case '2':
67 dec = (int) Math.pow(8, j) * 2;
68 break;
69 case '3':
70 dec = (int) Math.pow(8, j) * 3;
71 break;
72 case '4':
73 dec = (int) Math.pow(8, j) * 4;
74 break;
75 case '5':
76 dec = (int) Math.pow(8, j) * 5;
77 break;
78 case '6':
79 dec = (int) Math.pow(8, j) * 6;
80 break;
81 case '7':
82 dec = (int) Math.pow(8, j) * 7;
83 break;
84 default:
85 value = false;
86
87 }
88
89 total += dec;
90
91 }
92 if (value) {
93 System.out.println("Decimal System: " + total);
94 } else {
95 System.out.println("Invalid value!");
96 }
97
92 if (value) {
93 System.out.println("Decimal System: " + total);
94 } else {
95 System.out.println("Invalid value!");
96 }
97
98 }
99
100 /**
101 * This method convert the number from the Hexadecimal to the Decimal.
102 */
103 public void HexToDec() //H2D
104 {
105 System.out.println("Enter Number in Hexdecimal System: ");
106 String hex = input.next();
107 int length = hex.length();
108 hex = hex.toLowerCase();
109 long total = 0;
110 boolean test = true;
111 for (int i = length - 1, j = 0; i >= 0; i--, j++) {
112 int dec = 0;
113
114 switch (hex.charAt(i)) {
115 case '1':
116 dec = (int) Math.pow(16, j);
117 break;
118 case '2':
119 dec = (int) Math.pow(16, j) * 2;
120 break;
121 case '3':
122 dec = (int) Math.pow(16, j) * 3;
123 break;
124 case '4':
125 dec = (int) Math.pow(16, j) * 4;
126 break;
127 case '5':
128 dec = (int) Math.pow(16, j) * 5;
129 break;
130 case '6':
131 dec = (int) Math.pow(16, j) * 6;
132 break;
133 case '7':
134 dec = (int) Math.pow(16, j) * 7;
135 break;
136 case '8':
137 dec = (int) Math.pow(16, j) * 8;
138 break;
139 case '9':
140 dec = (int) Math.pow(16, j) * 9;
141 break;
142 case 'a':
143 dec = (int) Math.pow(16, j) * 10;
144 break;
139 case '9':
140 dec = (int) Math.pow(16, j) * 9;
141 break;
142 case 'a':
143 dec = (int) Math.pow(16, j) * 10;
144 break;
145 case 'b':
146 dec = (int) Math.pow(16, j) * 11;
147 break;
148 case 'c':
149 dec = (int) Math.pow(16, j) * 12;
150 break;
151 case 'd':
152 dec = (int) Math.pow(16, j) * 13;
153 break;
154 case 'e':
155 dec = (int) Math.pow(16, j) * 14;
156 break;
157 case 'f':
158 dec = (int) Math.pow(16, j) * 15;
159 break;
160 case '0':
161 dec = 0;
162 break;
163 default:
164 test = false;
165 break;
166 }
167
168 total += dec;
169
170 }
171 if (test) {
172 System.out.println(total);
173 } else {
174 System.out.println("Invalid value!");
175 }
176
177 }
178
179 /**
180 * This method convert the number from the Decimal to the Binary.
181 */
182 public void DecToBin() //D2B
183 {
184 System.out.println("Enter Number in Decimal System: ");
185 if (input.hasNextLong()) {
186 long dec = input.nextLong();
187 String binary = "";
188 if (dec > 0) {
189 while (dec > 0) {
190 String bin = dec % 2 + "";
191 dec = dec / 2;
186 long dec = input.nextLong();
187 String binary = "";
188 if (dec > 0) {
189 while (dec > 0) {
190 String bin = dec % 2 + "";
191 dec = dec / 2;
192 binary = bin + binary;
193 }
194 } else if (dec == 0) {
195 binary = "0";
196 } else {
197 System.out.print("Invalid value! ");
198 }
199 System.out.println(binary);
200 } else {
201 System.out.println("Invalid value! ");
202 }
203 }
204
205 /**
206 * This method convert the number from the Octal to the Binary.
207 */
208 public void OctToBin() //O2B
209 {
210 System.out.println("Enter Number in Octal System:");
211 String OctNumber = input.next();
212
213 long total = 0;
214
215 String oct = OctNumber;
216 int length = oct.length();
217 boolean value = true;
218
219 for (int i = length - 1, j = 0; i >= 0; i--, j++) {
220 int dec = 0;
221
222 switch (oct.charAt(i)) {
223 case '0':
224 dec = 0;
225 break;
226 case '1':
227 dec = (int) Math.pow(8, j);
228 break;
229 case '2':
230 dec = (int) Math.pow(8, j) * 2;
231 break;
232 case '3':
233 dec = (int) Math.pow(8, j) * 3;
234 break;
235 case '4':
236 dec = (int) Math.pow(8, j) * 4;
237 break;
238 case '5':
233 dec = (int) Math.pow(8, j) * 3;
234 break;
235 case '4':
236 dec = (int) Math.pow(8, j) * 4;
237 break;
238 case '5':
239 dec = (int) Math.pow(8, j) * 5;
240 break;
241 case '6':
242 dec = (int) Math.pow(8, j) * 6;
243 break;
244 case '7':
245 dec = (int) Math.pow(8, j) * 7;
246 break;
247 default:
248 value = false;
249
250 }
251
252 total += dec;
253
254 }
255 if (value) {
256 String binary = "";
257 if (total > 0) {
258 while (total > 0) {
259 String bin = total % 2 + "";
260 total = total / 2;
261 binary = bin + binary;
262 }
263 } else if (total == 0) {
264 binary = "0";
265 } else {
266 System.out.print("Invalid value! ");
267 }
268 System.out.println(binary);
269 } else {
270 System.out.println("Invalid value! ");
271 }
272
273 }
274
275 /**
276 * This method convert the number from the Hexadecimal to the Binary.
277 */
278 public void HexToBin() //H2B
279 {
280
281 System.out.println("Enter Number in Hexdecimal System: ");
282 String hex = input.next();
283 int length = hex.length();
284 hex = hex.toLowerCase();
285 long total = 0;
280
281 System.out.println("Enter Number in Hexdecimal System: ");
282 String hex = input.next();
283 int length = hex.length();
284 hex = hex.toLowerCase();
285 long total = 0;
286 boolean test = true;
287 for (int i = length - 1, j = 0; i >= 0; i--, j++) {
288 int dec = 0;
289
290 switch (hex.charAt(i)) {
291 case '1':
292 dec = (int) Math.pow(16, j);
293 break;
294 case '2':
295 dec = (int) Math.pow(16, j) * 2;
296 break;
297 case '3':
298 dec = (int) Math.pow(16, j) * 3;
299 break;
300 case '4':
301 dec = (int) Math.pow(16, j) * 4;
302 break;
303 case '5':
304 dec = (int) Math.pow(16, j) * 5;
305 break;
306 case '6':
307 dec = (int) Math.pow(16, j) * 6;
308 break;
309 case '7':
310 dec = (int) Math.pow(16, j) * 7;
311 break;
312 case '8':
313 dec = (int) Math.pow(16, j) * 8;
314 break;
315 case '9':
316 dec = (int) Math.pow(16, j) * 9;
317 break;
318 case 'a':
319 dec = (int) Math.pow(16, j) * 10;
320 break;
321 case 'b':
322 dec = (int) Math.pow(16, j) * 11;
323 break;
324 case 'c':
325 dec = (int) Math.pow(16, j) * 12;
326 break;
327 case 'd':
328 dec = (int) Math.pow(16, j) * 13;
329 break;
330 case 'e':
331 dec = (int) Math.pow(16, j) * 14;
332 break;
327 case 'd':
328 dec = (int) Math.pow(16, j) * 13;
329 break;
330 case 'e':
331 dec = (int) Math.pow(16, j) * 14;
332 break;
333 case 'f':
334 dec = (int) Math.pow(16, j) * 15;
335 break;
336 case '0':
337 dec = 0;
338 break;
339 default:
340 test = false;
341 break;
342 }
343
344 total += dec;
345
346 }
347 if (test) {
348 String binary = "";
349 if (total > 0) {
350 while (total > 0) {
351 String bin = total % 2 + "";
352 total = total / 2;
353 binary = bin + binary;
354 }
355 } else if (total == 0) {
356 binary = "0";
357 } else {
358 System.out.print("Invalid value! ");
359 }
360 System.out.println(binary);
361 } else {
362 System.out.println("Invalid value! ");
363 }
364
365 }
366
367 /**
368 * This method convert the number from the Decimal to the Octal.
369 */
370 public void DecToOct() //D2O
371 {
372 System.out.println("Enter Number in Decimal System: ");
373 if (input.hasNextLong()) {
374 long dec = input.nextLong();
375 String octal = "";
376 if (dec > 0) {
377 while (dec > 0) {
378 String oct = dec % 8 + "";
379 dec = dec / 8;
374 long dec = input.nextLong();
375 String octal = "";
376 if (dec > 0) {
377 while (dec > 0) {
378 String oct = dec % 8 + "";
379 dec = dec / 8;
380 octal = oct + octal;
381 }
382
383 } else if (dec == 0) {
384 octal = "0";
385 } else {
386 System.out.print("Invalid value! ");
387 }
388 System.out.println(octal);
389 } else {
390 System.out.println("Invalid value! ");
391 }
392 }
393
394 /**
395 * This method convert the number from the Binary to the Octal.
396 */
397 public void BinToOct() //B2O
398 {
399 System.out.println("Enter Number in Binary System: ");
400 String bin = input.next();
401 int length = bin.length();
402 int total = 0;
403 for (int i = length - 1, j = 0; i >= 0; i--, j++) {
404 int dec = 0;
405 if (!(bin.charAt(i) == '0' || bin.charAt(i) == '1')) {
406 System.out.println("Invalid value! ");
407 break;
408 } else {
409 switch (bin.charAt(i)) {
410 case '1':
411 dec = (int) Math.pow(2, j);
412 break;
413 case '0':
414 dec = 0;
415 break;
416
417 }
418 total += dec;
419 if (i == 0) {
420 String octal = "";
421 if (total > 0) {
422 while (total > 0) {
423 String oct = total % 8 + "";
424 total = total / 8;
425 octal = oct + octal;
426 }
421 if (total > 0) {
422 while (total > 0) {
423 String oct = total % 8 + "";
424 total = total / 8;
425 octal = oct + octal;
426 }
427
428 } else if (total == 0) {
429 octal = "0";
430 } else {
431 System.out.print("Invalid value! ");
432 }
433 System.out.println(octal);
434 }
435 }
436 }
437
438 }
439
440 /**
441 * This method convert the number from the Hexadecimal to the Octal.
442 */
443 public void HexToOct() //H2O
444 {
445 System.out.println("Enter Number in Hexdecimal System: ");
446 String hex = input.next();
447 int length = hex.length();
448 hex = hex.toLowerCase();
449 long total = 0;
450 boolean test = true;
451 for (int i = length - 1, j = 0; i >= 0; i--, j++) {
452 int dec = 0;
453
454 switch (hex.charAt(i)) {
455 case '1':
456 dec = (int) Math.pow(16, j);
457 break;
458 case '2':
459 dec = (int) Math.pow(16, j) * 2;
460 break;
461 case '3':
462 dec = (int) Math.pow(16, j) * 3;
463 break;
464 case '4':
465 dec = (int) Math.pow(16, j) * 4;
466 break;
467 case '5':
468 dec = (int) Math.pow(16, j) * 5;
469 break;
470 case '6':
471 dec = (int) Math.pow(16, j) * 6;
472 break;
473 case '7':
468 dec = (int) Math.pow(16, j) * 5;
469 break;
470 case '6':
471 dec = (int) Math.pow(16, j) * 6;
472 break;
473 case '7':
474 dec = (int) Math.pow(16, j) * 7;
475 break;
476 case '8':
477 dec = (int) Math.pow(16, j) * 8;
478 break;
479 case '9':
480 dec = (int) Math.pow(16, j) * 9;
481 break;
482 case 'a':
483 dec = (int) Math.pow(16, j) * 10;
484 break;
485 case 'b':
486 dec = (int) Math.pow(16, j) * 11;
487 break;
488 case 'c':
489 dec = (int) Math.pow(16, j) * 12;
490 break;
491 case 'd':
492 dec = (int) Math.pow(16, j) * 13;
493 break;
494 case 'e':
495 dec = (int) Math.pow(16, j) * 14;
496 break;
497 case 'f':
498 dec = (int) Math.pow(16, j) * 15;
499 break;
500 case '0':
501 dec = 0;
502 break;
503 default:
504 test = false;
505 break;
506 }
507
508 total += dec;
509
510 }
511 if (test) {
512 //////
513 String octal = "";
514 if (total > 0) {
515 while (total > 0) {
516 String oct = total % 8 + "";
517 total = total / 8;
518 octal = oct + octal;
519 }
520
515 while (total > 0) {
516 String oct = total % 8 + "";
517 total = total / 8;
518 octal = oct + octal;
519 }
520
521 } else if (total == 0) {
522 octal = "0";
523 } else {
524 System.out.print("Invalid value! ");
525 }
526 System.out.println(octal);
527 //////
528 } else {
529 System.out.println("Invalid value!");
530 }
531
532 }
533
534 /**
535 * This method convert the number from the Decimal to the Hexadecimal.
536 */
537 public void DecToHex() //D2H
538 {
539 System.out.println("Enter Number in Decimal System: ");
540 if (input.hasNextLong()) {
541 long dec = input.nextLong();
542 String hex = "";
543 if (dec > 0) {
544 while (dec > 0) {
545 int mod = (int) dec % 16;
546 dec = dec / 16;
547 switch (mod) {
548 case 0:
549 hex = "0" + hex;
550 break;
551 case 1:
552 hex = "1" + hex;
553 break;
554 case 2:
555 hex = "2" + hex;
556 break;
557 case 3:
558 hex = "3" + hex;
559 break;
560 case 4:
561 hex = "4" + hex;
562 break;
563 case 5:
564 hex = "5" + hex;
565 break;
566 case 6:
567 hex = "6" + hex;
562 break;
563 case 5:
564 hex = "5" + hex;
565 break;
566 case 6:
567 hex = "6" + hex;
568 break;
569 case 7:
570 hex = "7" + hex;
571 break;
572 case 8:
573 hex = "8" + hex;
574 break;
575 case 9:
576 hex = "9" + hex;
577 break;
578 case 10:
579 hex = "A" + hex;
580 break;
581 case 11:
582 hex = "B" + hex;
583 break;
584 case 12:
585 hex = "C" + hex;
586 break;
587 case 13:
588 hex = "D" + hex;
589 break;
590 case 14:
591 hex = "E" + hex;
592 break;
593 case 15:
594 hex = "F" + hex;
595 break;
596 }
597 }
598 System.out.println(hex);
599
600 } else if (dec == 0) {
601 System.out.println("0");
602 } else {
603 System.out.println("Invalid value! ");
604 }
605 } else {
606 System.out.println("Invalid value! ");
607 }
608 }
609
610 /**
611 * This method convert the number from the Binary to the Hexadecimal.
612 */
613 public void BinToHex() //B2H
614 {
609
610 /**
611 * This method convert the number from the Binary to the Hexadecimal.
612 */
613 public void BinToHex() //B2H
614 {
615 System.out.println("Enter Number in Binary System: ");
616 String bin = input.next();
617 int length = bin.length();
618 int total = 0;
619 for (int i = length - 1, j = 0; i >= 0; i--, j++) {
620 int dec = 0;
621 if (!(bin.charAt(i) == '0' || bin.charAt(i) == '1')) {
622 System.out.println("Invalid value! ");
623 break;
624 } else {
625 switch (bin.charAt(i)) {
626 case '1':
627 dec = (int) Math.pow(2, j);
628 break;
629 case '0':
630 dec = 0;
631 break;
632
633 }
634 total += dec;
635 if (i == 0) {
636 String hex = "";
637
638 if (total > 0) {
639 while (total > 0) {
640 int mod = (int) total % 16;
641 total = total / 16;
642 switch (mod) {
643 case 0:
644 hex = "0" + hex;
645 break;
646 case 1:
647 hex = "1" + hex;
648 break;
649 case 2:
650 hex = "2" + hex;
651 break;
652 case 3:
653 hex = "3" + hex;
654 break;
655 case 4:
656 hex = "4" + hex;
657 break;
658 case 5:
659 hex = "5" + hex;
660 break;
661 case 6:
656 hex = "4" + hex;
657 break;
658 case 5:
659 hex = "5" + hex;
660 break;
661 case 6:
662 hex = "6" + hex;
663 break;
664 case 7:
665 hex = "7" + hex;
666 break;
667 case 8:
668 hex = "8" + hex;
669 break;
670 case 9:
671 hex = "9" + hex;
672 break;
673 case 10:
674 hex = "A" + hex;
675 break;
676 case 11:
677 hex = "B" + hex;
678 break;
679 case 12:
680 hex = "C" + hex;
681 break;
682 case 13:
683 hex = "D" + hex;
684 break;
685 case 14:
686 hex = "E" + hex;
687 break;
688 case 15:
689 hex = "F" + hex;
690 break;
691 }
692 }
693 System.out.println(hex);
694
695 } else if (total == 0) {
696 System.out.println("0");
697 } else {
698 System.out.println("Invalid value! ");
699 }
700 }
701 }
702 }
703
704 }
705
706 /**
707 * This method convert the number from the Octal to the Hexadecimal.
708 */
703
704 }
705
706 /**
707 * This method convert the number from the Octal to the Hexadecimal.
708 */
709 public void OctToHex() //O2H
710 {
711 System.out.println("Enter Number in Octal System:");
712 String OctNumber = input.next();
713
714 int total = 0;
715
716 String oct = OctNumber;
717 int length = oct.length();
718 boolean value = true;
719
720 for (int i = length - 1, j = 0; i >= 0; i--, j++) {
721 int dec = 0;
722
723 switch (oct.charAt(i)) {
724 case '0':
725 dec = 0;
726 break;
727 case '1':
728 dec = (int) Math.pow(8, j);
729 break;
730 case '2':
731 dec = (int) Math.pow(8, j) * 2;
732 break;
733 case '3':
734 dec = (int) Math.pow(8, j) * 3;
735 break;
736 case '4':
737 dec = (int) Math.pow(8, j) * 4;
738 break;
739 case '5':
740 dec = (int) Math.pow(8, j) * 5;
741 break;
742 case '6':
743 dec = (int) Math.pow(8, j) * 6;
744 break;
745 case '7':
746 dec = (int) Math.pow(8, j) * 7;
747 break;
748 default:
749 value = false;
750
751 }
752
753 total += dec;
754
755 }
750
751 }
752
753 total += dec;
754
755 }
756 if (value) {
757 String hex = "";
758 if (total > 0) {
759 while (total > 0) {
760 int mod = (int) total % 16;
761 total = total / 16;
762 switch (mod) {
763 case 0:
764 hex = "0" + hex;
765 break;
766 case 1:
767 hex = "1" + hex;
768 break;
769 case 2:
770 hex = "2" + hex;
771 break;
772 case 3:
773 hex = "3" + hex;
774 break;
775 case 4:
776 hex = "4" + hex;
777 break;
778 case 5:
779 hex = "5" + hex;
780 break;
781 case 6:
782 hex = "6" + hex;
783 break;
784 case 7:
785 hex = "7" + hex;
786 break;
787 case 8:
788 hex = "8" + hex;
789 break;
790 case 9:
791 hex = "9" + hex;
792 break;
793 case 10:
794 hex = "A" + hex;
795 break;
796 case 11:
797 hex = "B" + hex;
798 break;
799 case 12:
800 hex = "C" + hex;
801 break;
802 case 13:
797 hex = "B" + hex;
798 break;
799 case 12:
800 hex = "C" + hex;
801 break;
802 case 13:
803 hex = "D" + hex;
804 break;
805 case 14:
806 hex = "E" + hex;
807 break;
808 case 15:
809 hex = "F" + hex;
810 break;
811 }
812 }
813 System.out.println(hex);
814
815 } else if (total == 0) {
816 System.out.println("0");
817 } else {
818 System.out.println("Invalid value! ");
819 }
820 } else {
821 System.out.println("Invalid value!");
822 }
823
824 }
825
826
827
828 public static void main(String[] args) {
829 // TODO code application logic here
830 SistemaNumerico user = new SistemaNumerico();
831 // From Binary to Another
832 user.BinToDec();
833 user.BinToHex();
834 user.BinToOct();
835
836 // From Decimal to Another
837 user.DecToBin();
838 user.DecToHex();
839 user.DecToOct();
840
841 // From Hexadecimal to Another
842 user.HexToBin();
843 user.HexToDec();
844 user.HexToOct();
845
846 // From Octal to Another
847 user.OctToBin();
848 user.OctToDec();
849 user.OctToHex();
844 user.HexToOct();
845
846 // From Octal to Another
847 user.OctToBin();
848 user.OctToDec();
849 user.OctToHex();
850 }
851 }

You might also like