博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java中四种进制的转换
阅读量:6722 次
发布时间:2019-06-25

本文共 3659 字,大约阅读时间需要 12 分钟。

  hot3.png

package com.chapter02;import java.util.Scanner;public class SysConvert {	/**	 * other system convert into ten system	 * 	 * @param a	 * @param str	 *   d	 */	public static int otherToTen(int a, String str) {		double d = 0; // number after convert		String subString; // loop acquire single element on the basis of the							// length of the string		for (int i = 0; i < str.length(); i++) {			subString = str.substring(i, i + 1); // loop cut the string			if (a == 16) {				subString = sixteenToNumber(subString); // convert the char to														// number			}			d += Integer.parseInt(subString)					* Math.pow(a, str.length() - i - 1);		}		return (int) d;	}	/**	 * convert ten system to other system	 * 	 * @param a	 * @param str	 *  	 */	public static String tenToNumber(int a, String str) {		int current = Integer.parseInt(str);		String opResult = "";		// Judge whether the number after convert is sixteen system		if (a == 16)		// Judge whether the current number is bigger than sixteen.		// If it is,when meet sixteen,carry a number 1		{			while (current >= a) {				opResult += sixteenNumberToChar(current % a);				current /= a;			}			if (current != 0)				opResult += sixteenNumberToChar(current);		} else {			// Judge whether the current number is bigger than the system after			// convert			while (current >= a) {				opResult += current % a;				current /= a;			}			if (current != 0)				opResult += current;		}		String riResult = ""; // binary system string of inverted order		// loop print out by binary system		for (int i = opResult.length() - 1; i >= 0; i--) {			riResult = riResult + opResult.substring(i, i + 1);		}		return riResult;	}	public static String sixteenToNumber(String s) {		String num = "";		if (s.equals("A") && s.equals("a")) {			num = "10";		} else if (s.equals("B") && s.equals("b")) {			num = "11";		} else if (s.equals("C") && s.equals("c")) {			num = "12";		} else if (s.equals("D") && s.equals("d")) {			num = "13";		} else if (s.equals("E") && s.equals("e")) {			num = "14";		} else if (s.equals("F") && s.equals("f")) {			num = "15";		} else {			num = s;		}		return num;	}	public static String sixteenNumberToChar(int num) {		// sixteenNumber convert to char		String c = "";		if (num == 10) {			c = "A";		} else if (num == 11) {			c = "B";		} else if (num == 12) {			c = "C";		} else if (num == 13) {			c = "D";		} else if (num == 14) {			c = "E";		} else if (num == 15) {			c = "F";		} else {			c = String.valueOf(num);		}		return c;	}	/**	 * main,the entry of this program	 * 	 * @param args	 */	public static void main(String[] args) {		String number; // the number about to convert		int a, b; // a means the number before convert		// b means the number after convert		String result;	//the result after convert		String stop;			Scanner read = new Scanner(System.in);		do{			System.out.println("Input three interger numbers:the number before convert,pre system,later system");			number = read.next();			a = read.nextInt();			b = read.nextInt();			stop = "Quit";		}while(stop != "Quit");				try{			if(a != 10)	//Judge whether the number before convert is ten system			{				String temp = String.valueOf(otherToTen(a,number));	//the number convert to ten system				result = String.valueOf(tenToNumber(b, temp));	//ten system convert to other system			}			else			{			result = String.valueOf(tenToNumber(b,number));	//ten system convert to other system			}			System.out.println(a + " system's number " + number + " convert to " + b + " system,\nthe result is " + result);		}catch(Exception e)		{			System.out.println("Failed to convert,please input correctly!");			System.exit(-1);		}	}}

转载于:https://my.oschina.net/clear/blog/61919

你可能感兴趣的文章
Python中用datetime包进行对时间的一些操作
查看>>
Web.xml配置详解之context-param
查看>>
WIKI扫盲手册
查看>>
Javascript - ExtJs - 整合百度文章编辑器
查看>>
JS分两种数据类型,你都知道吗?
查看>>
Bash条件判断
查看>>
JSF 2.0 + Ajax hello world example
查看>>
Firefox 插件 FlashGot 创建 Axel 下载任务
查看>>
php中相对路径和绝对路径如何使用(详解)
查看>>
Oracle 数据库、实例、用户、表空间、表之间的关系
查看>>
RVM 多版本Ruby管理-Gentoo
查看>>
android error: Apostrophe not preceded by \
查看>>
小程序-scroll-view下拉事件请求多次接口
查看>>
AngularJs 指令
查看>>
云适配正则笔记
查看>>
开始学习silverlight
查看>>
php使用递归计算目录大小
查看>>
EF 直接修改数据,不再查询数据库
查看>>
script标签加载js代码的一些知识
查看>>
The builder launch configuration could not be found
查看>>