java 10进制转成16进制
时间: 2024-01-07 07:04:59 浏览: 120
java 十进制转为十六进制
5星 · 资源好评率100%
以下是两种Java实现10进制转16进制的方法:
1.使用Integer.toHexString()方法:
```java
int a = 15;
String hex = Integer.toHexString(a);
System.out.println(hex); // 输出:f
```
2.手动实现转换:
```java
Scanner input = new Scanner(System.in);
int a = input.nextInt();
int i, m, b, k;
char[] sum = new char[10];
k = a;
char[] s = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
for(i = 0; i < 10; i++){
b = a % 16;
sum[i] = s[b];
a = a / 16;
if(a == 0) break;
}
m = i;
System.out.print(k + "'s hex representation is ");
for(; m >= 0; m--)
System.out.printf("%c", sum[m]);
```
阅读全文