public class Zhuan{
public static void main(String[] args)
{
System.out.println(decToHex(1958));//把1958转为16进制
//字符串翻转
public static String reverse(String s)
{
int length=s.length();
String r="";
for(int i=length-1;i>=0;i–)
{
r+=s.charAt(i);
}
return r;
}
public static String decToHex(int n)
{
String r= “”;
while(n>16)
{
int yushu=n%16;
int shang=n/16;
if(yushu>9)
{
char c=(char)((yushu-10)+‘A’);//细节:注意括号加的位置
r=r+c;
}else
{
r=r+yushu;
}
n=shang;
}
if(n>9)
{
char c=(char)((n-10)+‘A’);
r+=c;
}
else
{
r+=n;
}
return reverse®;
}
}