This article describes the implementation of complex binary converter functions in Java. Share it for your reference, as follows:
This is a java-written binary converter, including 12 types of mutual conversions between 10, 2, 8 and hexadecimal. After entering a number to be converted, select the conversion method you want to use according to the prompts and output the conversion result.
Note: The newly uploaded file has been improved based on the previous code (3. original java code below), which can better implement encapsulation.
1. SystemConvert.java
package day8;import java.util.Scanner;public class SystemConvert { static Scanner scanner = new Scanner(System.in); static String s = ""; // Define the method of decimal to binary. public static String C10T2(int numb) { String result = ""; for (int i = numb; i > 0; i /= 2) result = i % 2 + result; return result; } // Define the method of decimal to octal. public static String C10T8(int numb) { String result = ""; for (int i = numb; i > 0; i /= 8) result = i % 8 + result; return result; } // Define the method of bin to decimal. public static int C2T10(int numb) { int k = 0, result = 0; // String result=null; for (int i = numb; i > 0; i /= 10) { result += (i % 10) * Math.pow(2, k); k++; } return result; } // Define the method of octal to decimal. public static int C8T10(int numb) { int k = 0, temp = 0; for (int i = numb; i > 0; i /= 10) { temp += (i % 10) * Math.pow(8, k); k++; } return temp; } public static void convert10(int numb, int to) { String s = ""; switch (to) { case 2: s = "" + C10T2(numb); break; case 8: s = "" + C10T8(numb); break; case 16: s = Integer.toHexString(numb).toUpperCase(); break; default: System.out.println("wrong input!"); } System.out.println(s); } public static void convert2(int numb, int to) { String s = ""; switch (to) { case 10: s = "" + C2T10(numb); break; case 8: s = "" + C10T8(C2T10(numb)); break; case 16: s = Integer.toHexString(C2T10(numb)).toUpperCase(); break; default: System.out.println("wrong input!"); } System.out.println(s); } public static void convert8(int numb, int to) { String s = ""; switch (to) { case 2: s = "" + C10T2(C8T10(numb)); break; case 10: s = "" + C8T10(numb); break; case 16: s = Integer.toHexString(C8T10(numb)).toUpperCase(); break; default: System.out.println("wrong input!"); } System.out.println(s); } public static void convert16(String numb, int to) { String s = ""; switch (to) { case 2: int temp2 = Integer.parseInt(numb, 16); s = C10T2(temp2); break; case 8: int temp3 = Integer.parseInt(numb, 16); s = C10T8(temp3); break; case 10: int temp = Integer.parseInt(numb, 16); s = "" + temp; break; default: System.out.println("wrong input!"); } System.out.println(s); } public static void convert(int numb, int from, int to) { switch (from) { case 10: convert10(numb, to); break; case 2: convert2(numb, to); break; case 8: convert8(numb, to); break; default: System.out.println("wrong input!"); } } public static void convert(String numb, int from, int to) { switch (from) { case 16: convert16(numb, to); break; default: System.out.println("wrong input!"); } } public static void main(String[] args) { System.out.println("Is the hexadecimal number to be converted? /n Enter 1. Represents yes; /n input 2. Represents not./n"); int input = scanner.nextInt(); switch (input) { case 1: System.out.println("Please enter a hexadecimal number: "); String numb = scanner.next(); System.out.println("What kind of binary number is converted to?"); int to = scanner.nextInt(); convert(numb, 16, to); break; case 2: System.out.println("Please enter a hexadecimal number: "); int numb2 = scanner.nextInt(); System.out.println("From what binary number?"); int from = scanner.nextInt(); System.out.println("What binary number to convert?"); int to2 = scanner.nextInt(); convert(numb2, from, to2); break; default: System.out.println("wrong input!"); } }}2. Screenshot of the running effect:
3. Original java code
import java.util.Scanner;public class SystemConvert { static Scanner scanner = new Scanner(System.in); static String s = ""; public static void convert() { System.out.println("please input a number:"); String number = scanner.next(); System.out.println("choose a way:/n Enter 1, indicating decimal to binary;/n" + "Input 2, indicating decimal to decimal;/n" + "Input 3, indicating decimal to octal;/n" + "Input 4, means ength to decimal;/n" + "Input 5, means decimal to hexadecimal;/n" + "Input 6, means hexadecimal to decimal;/n" + "Input 7, means german to ength;/n" + "Input 8, means german to hexadecimal;/n" + "Input 9, means ength to hexadecimal;/n" + "Input 10, means ength to hexadecimal;/n" + "Input 11, means hexadecimal to bin;/n" + "Input 12, means hexadecimal to octal;/n"); int input = scanner.nextInt(); switch (input) { case 1: // 10>>>2 s = "" + C10T2(numb); break; case 2: // 2>>>10 s += C2T10(numb); break; case 3: // 10>>>8 s = "" + C10T8(numb); break; case 4: // 8>>>10 s = "" + C8T10(numb); break; case 5: // 10>>>16 s = Integer.toHexString(Integer.valueOf(numb)).toUpperCase(); break; case 6:// 16>>>10 int temp = Integer.parseInt(numb, 16); s = "" + temp; break; case 7: // 2>>>8 s = "" + C10T8(Integer.toString(C2T10(numb))); break; case 8: // 2>>>16 s = Integer.toHexString(Integer.valueOf(C2T10(numb))).toUpperCase(); break; case 9: // 8>>>2 s = "" + C10T2(Integer.toString(C8T10(numb))); break; case 10:// 8>>>16 s = Integer.toHexString(Integer.valueOf(C8T10(numb))).toUpperCase(); break; case 11:// 16>>>2 int temp2 = Integer.parseInt(numb, 16); s = Integer.toBinaryString(temp2); break; case 12:// 16>>>8 int temp3 = Integer.parseInt(numb, 16); s = C10T8(Integer.toString(temp3)); break; default: System.out.println("Wrong input!"); } System.out.println(s); } public static int C2T10(String numb) { int k = 0, result = 0; // String result=null; for (int i = Integer.valueOf(numb); i > 0; i /= 10) { result += (i % 10) * Math.pow(2, k); k++; } return result; } public static int C8T10(String numb) { int k = 0, temp = 0; for (int i = Integer.valueOf(numb); i > 0; i /= 10) { temp += (i % 10) * Math.pow(8, k); k++; } return temp; } public static String C10T8(String numb) { String result = ""; for (int i = Integer.valueOf(numb); i > 0; i /= 8) result = i % 8 + result; return result; } public static String C10T2(String numb) { String result = ""; for (int i = Integer.valueOf(numb); i > 0; i /= 2) result = i % 2 + result; return result; } public static void main(String[] args) { SystemConvert.convert(); }}4. Run screenshot:
PS: Here are a few online conversion and calculation tools for this website. I believe it will be helpful to you:
Online arbitrary conversion tool:
http://tools.VeVB.COM/transcoding/hexconvert
Online Standard Calculator:
http://tools.VeVB.COM/jisuanqi/jsq
Online scientific calculator:
http://tools.VeVB.COM/jisuanqi/jsqkeexue
I hope this article will be helpful to everyone's Java programming.