我需要将除以 16 的其余部分转换为十六进制。我正在使用 Integer.toHexString
方法并传递我需要转换为十六进制的值(变量“resto”),但输出值不是十六进制。
int total = 50;
resto = total / 16;
String decimal = Integer.toHexString(resto);
System.out.println(decimal);
输出 3
最佳答案
使用 Integer 类的 toHexString() 方法。
例子:
import java.util.Scanner;
class DecimalToHexExample
{
public static void main(String args[])
{
Scanner input = new Scanner( System.in );
System.out.print("Enter a decimal number : ");
int num =input.nextInt();
// calling method toHexString()
String str = Integer.toHexString(num);
System.out.println("Decimal to hexadecimal: "+str);
}
}
输出:
Enter a decimal number : 123
Decimal to hexadecimal: 7b
关于java - java中十进制转十六进制的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49928630/