十进制转十六进制
package choice;
import java.util.Scanner;
public class Choose {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
while(input.hasNextInt()){
int num=input.nextInt();
System.out.println(hexToDecimal(num));
}
input.close();
}
public static String hexToDecimal(int hex){
String d="";
while(hex>0){
int t = hex % 16;
d = (char)hexcharToDecimal(t) +d;
hex /=16;
}
return d;
}
public static int hexcharToDecimal(int ch){
if(ch>=0&&ch<10)
return ch+48;
else
return ch -9+96;
}
}
**********************************************************************************************************************
十六进制转十进制
package choice;
import java.util.Scanner;
public class Choose {
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
String hex =input.nextLine();
System.out.println(hexToDecimal(hex.toUpperCase()));
}
public static int hexToDecimal(String hex){
int decimalValue = 0;
for(int i =0 ; i< hex.length();i++){
char hexChar = hex.charAt(i);
decimalValue = decimalValue * 16 +hexCharToDecimal(hexChar);
}
return decimalValue;
}
public static int hexCharToDecimal(char ch){
if(ch >='A' && ch <='F')
return 10 + ch - 'A';
else
return ch - '0';
}
}