系统提供的进制转换为Intege.toBinaryString();等等一系列的方法重载,下面是我自己写的代码转换
package com.be.four;
public class Change {
public static void main(String[] args) {
shi_er(60);
}
/*
* 十进制转换为二进制
*/
public static void ten_two(int a){
StringBuffer sb=new StringBuffer();
while(a>0){
sb.append(a%2);
a=a/2;
}
System.out.println(sb.reverse());
}
/*
* 十六进制转换为二进制
*/
public static void sixteen_two(int a){
StringBuffer sb=new StringBuffer();
while(a>0){
sb.append(a%16);
a=a/16;
}
System.out.println(sb.reverse());
}
/*
* 十进制转换为十六进制
*/
public static void ten_sixteen(int a){
StringBuffer sb=new StringBuffer();
for (int i = 0; i < 8; i++) {
int temp=a&15;
if(temp>9){
sb.append((char)(temp-10+'a'));
}else{
sb.append(temp);
}
a=a>>>4;
}
System.out.println(sb.reverse());
}
/*
* 十进制转换为十六进制
* 查表法
*/
public static void ten_sixteen_select(int a){
char[] sixteen={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
StringBuffer sb=new StringBuffer();
for (int i = 0; i < 8; i++) {
int temp=a&15;
sb.append(sixteen[temp]);
a=a>>>4;
}
System.out.println(sb.reverse());
}
/*
* 十进制转换为十六进制
* 查表法2,不用StringBuffer,用数组
*/
public static void ten_sixteen_select_2(int a){
char[] sixteen={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
char[]cunchu=new char[8];
int add=cunchu.length;
while(a!=0){
int temp=a&15;
cunchu[--add]=sixteen[temp];
a=a>>>4;
}
System.out.println(add);
for (int i = add; i <cunchu.length; i++) {
System.out.print(cunchu[i]+",");
}
}
/*
* 十进制转换为二进制
* 查表法,不用StringBuffer,用数组
*/
public static void ten_two_select(int a){
char[] two={'0','1'};
char[]cunchu=new char[32];
int add=cunchu.length;
while(a!=0){
int temp=a&1;
cunchu[--add]=two[temp];
a=a>>>1;
}
System.out.println(add);
for (int i = add; i <cunchu.length; i++) {
System.out.print(cunchu[i]+",");
}
}
/*
* 10------16
*/
public static void shi_shiliu(int num){
public_change(num, 15, 4);
}
/*
* 10------8
*/
public static void shi_ba(int num){
public_change(num, 7, 3);
}
/*
* 10------2
*/
public static void shi_er(int num){
public_change(num, 1, 1);
}
/*
* 总结,抽取,查表法
*/
public static void public_change(int num,int b,int right){
char shuzu[]={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
char[] arr=new char[32];
int move=arr.length;
while(num!=0){
int temp=num&b;
arr[--move]=shuzu[temp];
num=num>>>right;//
}
for (int i = move; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
}
public static void main(String[] args) {
shi_er(60);
}
/*
* 十进制转换为二进制
*/
public static void ten_two(int a){
StringBuffer sb=new StringBuffer();
while(a>0){
sb.append(a%2);
a=a/2;
}
System.out.println(sb.reverse());
}
/*
* 十六进制转换为二进制
*/
public static void sixteen_two(int a){
StringBuffer sb=new StringBuffer();
while(a>0){
sb.append(a%16);
a=a/16;
}
System.out.println(sb.reverse());
}
/*
* 十进制转换为十六进制
*/
public static void ten_sixteen(int a){
StringBuffer sb=new StringBuffer();
for (int i = 0; i < 8; i++) {
int temp=a&15;
if(temp>9){
sb.append((char)(temp-10+'a'));
}else{
sb.append(temp);
}
a=a>>>4;
}
System.out.println(sb.reverse());
}
/*
* 十进制转换为十六进制
* 查表法
*/
public static void ten_sixteen_select(int a){
char[] sixteen={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
StringBuffer sb=new StringBuffer();
for (int i = 0; i < 8; i++) {
int temp=a&15;
sb.append(sixteen[temp]);
a=a>>>4;
}
System.out.println(sb.reverse());
}
/*
* 十进制转换为十六进制
* 查表法2,不用StringBuffer,用数组
*/
public static void ten_sixteen_select_2(int a){
char[] sixteen={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
char[]cunchu=new char[8];
int add=cunchu.length;
while(a!=0){
int temp=a&15;
cunchu[--add]=sixteen[temp];
a=a>>>4;
}
System.out.println(add);
for (int i = add; i <cunchu.length; i++) {
System.out.print(cunchu[i]+",");
}
}
/*
* 十进制转换为二进制
* 查表法,不用StringBuffer,用数组
*/
public static void ten_two_select(int a){
char[] two={'0','1'};
char[]cunchu=new char[32];
int add=cunchu.length;
while(a!=0){
int temp=a&1;
cunchu[--add]=two[temp];
a=a>>>1;
}
System.out.println(add);
for (int i = add; i <cunchu.length; i++) {
System.out.print(cunchu[i]+",");
}
}
/*
* 10------16
*/
public static void shi_shiliu(int num){
public_change(num, 15, 4);
}
/*
* 10------8
*/
public static void shi_ba(int num){
public_change(num, 7, 3);
}
/*
* 10------2
*/
public static void shi_er(int num){
public_change(num, 1, 1);
}
/*
* 总结,抽取,查表法
*/
public static void public_change(int num,int b,int right){
char shuzu[]={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
char[] arr=new char[32];
int move=arr.length;
while(num!=0){
int temp=num&b;
arr[--move]=shuzu[temp];
num=num>>>right;//
}
for (int i = move; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
}