关于二进制,十进制,八进制,十六进制的转化规则:http://jingyan.baidu.com/article/495ba84109665338b30ede98.html
(1)二进制转成十进制
/**
* 将二进制数转化成十进制数
* @param num 二进制数 110 = 0*2的0次方+1*2的1次方+1*2的2次方
* @return 十进制数
*/
public static int TwotoTen(String num){
int result = 0;
String nums[] = num.split("");
for(int i = 0;i < nums.length;i++){
//Math.pow(2,i)计算的是2的i次方
result += Math.pow(2,i)*Double.valueOf(nums[nums.length-1-i]);
}
return result;
}
(2)十进制转成二进制
/**
* 将十进制数转化成二进制数
* @param num 十进制数
* @return 二进制数
*/
public static String TentoTwo(int num){
Stack stack = new Stack();
while(num != 0){
stack.push(num%2);
num = num /2;
}
StringBuffer sb = new StringBuffer("");
while(!stack.isEmpty()){
sb.append(stack.pop());
}
return sb.toString();
}
(3)二进制转成八进制
/**
* 二进制转化成八进制
* @param num 二进制数
* (1 0)( 0 1 0)( 1 1 0)
*
*/
public static String TwotoEight(String num){
String nums[] = num.split("");
Stack stack = new Stack();
for(int i = nums.length - 1;i >= 0;i = i - 3){
int ele = (int) (Math.pow(2,0)*Integer.valueOf(nums[i])) ;
if( i - 1 >= 0){
ele += Math.pow(2,1)*Integer.valueOf(nums[i-1]);
}
if(i - 2 >= 0){
ele += Math.pow(2,2)*Integer.valueOf(nums[i-2]);
}
stack.push(ele);
}
StringBuffer sb = new StringBuffer("");
while(!stack.isEmpty()){
sb.append(stack.pop());
}
return sb.toString();
}
(4)八进制转成二进制
/**
* 八进制转成二进制
* @param num 八进制数
*/
public static String EighttoTwo(String num){
LinkedList list = new LinkedList();
String nums[] = num.split("");
for(int i = 0;i < nums.length;i++){
int ele = Integer.valueOf(nums[nums.length-1-i]);
int len = 0;
while(ele != 0){
list.offerFirst(ele%2);
ele = ele/2;
len++;
}
if(len < 3){
list.offerFirst(0);
}
if(len < 2){
list.offerFirst(0);
}
if(len < 1){
list.offerFirst(0);
}
}
StringBuffer sb = new StringBuffer("");
while(!list.isEmpty()){
sb.append(list.pop());
}
return sb.toString();
}
(5)二进制转成十六进制
/**
* 二进制转成十六进制
* @param num 二进制数
*/
public static String TwotoSixteen(String num){
Stack stack = new Stack();
String nums[] = num.split("");
for(int i = nums.length-1;i >= 0;i = i - 4){
int ele = (int) (Math.pow(2,0) * Integer.valueOf(nums[i]));
if(i - 1 >= 0){
ele += Math.pow(2,1) * Integer.valueOf(nums[i-1]);
}
if(i - 2 >= 0){
ele += Math.pow(2,2) * Integer.valueOf(nums[i-2]);
}
if(i - 3 >= 0){
ele += Math.pow(2,3) * Integer.valueOf(nums[i-3]);
}
if(15 == ele){
stack.push("F");
}else if(14 == ele){
stack.push("E");
}else if(13 == ele){
stack.push("D");
}else if(12 == ele){
stack.push("C");
}else if(11 == ele){
stack.push("B");
}else if(10 == ele){
stack.push("A");
}else{
stack.push(ele);
}
}
StringBuffer sb = new StringBuffer("");
while(!stack.isEmpty()){
sb.append(stack.pop());
}
return sb.toString();
}
(6)十六进制转成二进制
/**
* 十六进制转成二进制
* @param num
*/
public static String SixteentoTwo(String num){
LinkedList list = new LinkedList();
String nums[] = num.split("");
for(int i = nums.length -1 ;i >= 0 ;i--){
int len = 0;
int ele = 0;
if(nums[i].equals("F")){
ele = 15;
}else if(nums[i].equals("E")){
ele = 14;
}else if(nums[i].equals("D")){
ele = 13;
}else if(nums[i].equals("C")){
ele = 12;
}else if(nums[i].equals("B")){
ele = 11;
}else if(nums[i].equals("A")){
ele = 10;
}else{
ele = Integer.valueOf(nums[i]);
}
while(ele != 0){
list.offerFirst(ele%2);
ele = ele/2;
len++;
}
if(len < 4){
list.offerFirst(0);
}
if(len < 3){
list.offerFirst(0);
}
if(len < 2){
list.offerFirst(0);
}
if(len < 1){
list.offerFirst(0);
}
}
return list.toString();
}
(7)八进制转成十进制:先把八进制转成二进制,再把二进制转成十进制
十进制转成八进制:先把十进制转成二进制,再把二进制转成八进制
八进制转成十六进制:先把八进制转成二进制,二进制再转成十六进制
十六进制转成八进制:先把十六进制转成二进制,再把二进制转成八进制
十进制转成十六进制:先把十进制转成二进制,再把二进制转成十六进制
十六进制转成十进制:先把十六进制转成二进制,再把二进制转成十进制
/**
* 八进制数转成十进制数
* @param num 八进制数
* 先把八进制数转成二进制数,再将二进制数转成十进制数
*/
public static int EighttoTen(String num){
String twoData = EighttoTwo(num);
int result = TwotoTen(twoData);
return result;
}
/**
* 十进制转成八进制
* @param num 十进制数
* @return 八进制数
*/
public static String TentoEight(int num){
String twoData = TentoTwo(num);
String result = TwotoEight(twoData);
return result;
}
/**
* 十六进制转成十进制
* @param num 十六进制数
* @return 十进制数
*/
public static int SixteentoTen(String num){
String twoData = SixteentoTwo(num);
int result = TwotoTen(twoData);
return result;
}
/**
* 十进制转成十六进制
* @param num
* @return
*/
public static String TentoSixteen(int num){
String twoData = TentoTwo(num);
String result = TwotoSixteen(twoData);
return result;
}
/**
* 八进制转成十六进制
* @param num 八进制
* @return 十六进制
*/
public static String EighttoSixteen(String num){
String twoData = EighttoTwo(num);
String result = TwotoSixteen(twoData);
return result;
}
/**
* 十六进制转成八进制
* @param num 十六进制数
* @return 八进制数
*/
public static String SixtteentoEight(String num){
String twoData = SixteentoTwo(num);
String result = TwotoEight(twoData);
return result;
}
(8)测试
//十进制转化成二进制
String result1 = TentoTwo(5);
System.out.println("result1 = " + result1);
//二进制转化成十进制
int result2 = TwotoTen("101");
System.out.println("result2 = " + result2);
//二进制转化成八进制
String result3 = TwotoEight("10010110");
System.out.println("result3 = " + result3);
//八进制转成二进制
String result4 = EighttoTwo("226");
System.out.println("result4 = " + result4);
//二进制转成十六进制
String result5 = TwotoSixteen("100101100");
System.out.println("result5 = " + result5);
String result6 = SixteentoTwo("12C");
System.out.println("result6 = " + result6);
(9)运行结果
result1 = 101
result2 = 5
result3 = 226
result4 = 010010110
result5 = 12C
result6 = [0, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0]