亲宝软件园·资讯

展开

Java进制转换器 Java实现复杂的进制转换器功能示例

aitaoke 人气:0
想了解Java实现复杂的进制转换器功能示例的相关内容吗,aitaoke在本文为您仔细讲解Java进制转换器的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Java,进制,转换,下面大家一起来学习吧。

本文实例讲述了Java实现复杂的进制转换器功能。分享给大家供大家参考,具体如下:

这是用java写的进制转换器,包括10、2、8、16进制之间共12种的相互转换。输入一个要转换的数之后,按提示选择所采用的转换方式,输出转换的结果。

注:新上传的文件是在此前代码(下面的3. 原java代码)的基础上做了改进,能更好地实现封装。

1. SystemConvert.java

package day8;
import java.util.Scanner;
public class SystemConvert {
  static Scanner scanner = new Scanner(System.in);
  static String s = "";
  // 定义10进制转2进制的方法。
  public static String C10T2(int numb) {
    String result = "";
    for (int i = numb; i > 0; i /= 2)
      result = i % 2 + result;
    return result;
  }
  // 定义10进制转8进制的方法。
  public static String C10T8(int numb) {
    String result = "";
    for (int i = numb; i > 0; i /= 8)
      result = i % 8 + result;
    return result;
  }
  // 定义2进制转10进制的方法。
  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;
  }
  // 定义8进制转10进制的方法。
  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("要转的是16进制数吗?\n输入1。代表是;\n输入2.代表不是.\n");
    int input = scanner.nextInt();
    switch (input) {
    case 1:
      System.out.println("请输入一个16进制数:");
      String numb = scanner.next();
      System.out.println("转成什么进制的数?");
      int to = scanner.nextInt();
      convert(numb, 16, to);
      break;
    case 2:
      System.out.println("请输入一个数:");
      int numb2 = scanner.nextInt();
      System.out.println("从什么进制数转起?");
      int from = scanner.nextInt();
      System.out.println("转成什么进制的数?");
      int to2 = scanner.nextInt();
      convert(numb2, from, to2);
      break;
    default:
      System.out.println("wrong input!");
    }
  }
}

2. 运行效果截图:

3. 原java代码

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 numb = scanner.next();
    System.out.println("choose a way:\n输入1,表示10进制转2进制;\n"
        + "输入2,表示2进制转10进制;\n" + "输入3,表示10进制转8进制;\n"
        + "输入4,表示8进制转10进制;\n" + "输入5,表示10进制转16进制;\n"
        + "输入6,表示16进制转10进制;\n" + "输入7,表示2进制转8进制;\n"
        + "输入8,表示2进制转16进制;\n" + "输入9,表示8进制转2进制;\n"
        + "输入10,表示8进制转16进制;\n" + "输入11,表示16进制转2进制;\n"
        + "输入12,表示16进制转8进制;\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. 运行截图:

PS:这里再为大家推荐几款本站的在线进制转换与计算工具,相信对于大家能有所帮助:

在线任意进制转换工具:
http://tools.softyun.net/transcoding/hexconvert

在线标准计算器:
http://tools.softyun.net/jisuanqi/jsq

在线科学计算器:
http://tools.softyun.net/jisuanqi/jsqkexue

希望本文所述对大家java程序设计有所帮助。

加载全部内容

相关教程
猜你喜欢
用户评论