import java.util.Scanner;
public class Decimal2HexConversion {
/**
* @param args
*/
public static void main(String[] args) {
//creat a scanner;
Scanner input =new Scanner(System.in);
//prompt the user to enter a decimal integer
System.out.print("Enter a decimal number:");
int decimal=input.nextInt();
System.out.println("the hex number for decimal"+decimal+"is"+decimalToHex(decimal));
}
//**Covert a decimal to a hex as a string*/
public static String decimalToHex(int decimal){
String hex="";
while(decimal!=0)
{int hexValue=decimal%16;
hex=ToHexChar(hexValue)+hex;
decimal=decimal/16;
}
return hex;
}
//covert an integer to a single hex digit in a character
public static char ToHexChar (int hexValue){
if(hexValue<=9&&hexValue >=0)
return (char)(hexValue+'0');
else//hexValue <=15&&hexValue>=10
return (char)(hexValue-10+'A');
}
}
将十进制数转换为十六进制
最新推荐文章于 2020-11-29 16:59:36 发布