在 Java 中将十进制(基数 10)转换为十六进制(基数 16)
Convert Decimal (Base 10) to Hexadecimal (Base 16) in Java
我的问题是关于 Java。
如何使用 Java 7 将 Java 中的十进制(基数 10)转换为十六进制(基数 16)?
当我在 C# 中使用方法 Convert.FromBase64String(str) with String "BQoPFBke" 我得到结果:05-0A-0F-14-19-1E
但是当我在 Java 中使用具有相同字符串的方法 Base64.Decoder.decode(str) 时,我得到了结果:[5, 10, 15, 20, 25, 30]
我尝试将十进制转换为十六进制:
public static String decimal2hex(int d) {
String digits = "0123456789ABCDEF";
if (d <= 0) return "0";
int base = 16; // flexible to change in any base under 16
String hex = "";
while (d > 0) {
int digit = d % base; // rightmost digit
hex = digits.charAt(digit) + hex; // string concatenation
d = d / base;
}
return hex;
}
但是当我使用例如 decima2hex(15) 方法时 returns 只有:F。但是我需要得到:0F.
如何实现?
请试试这个。
十六进制 = hex.length() <= 1 ? String.format("0%s",十六进制) : 十六进制;
输入输出完成程序
public class App
{
public static String decimal2hex(int d)
{
String digits = "0123456789ABCDEF";
if (d <= 0) return "0";
int base = 16; // flexible to change in any base under 16
String hex = "";
while (d > 0)
{
int digit = d % base; // rightmost digit
hex = digits.charAt(digit) + hex; // string concatenation
d = d / base;
}
hex = hex.length() <= 1 ? String.format("0%s", hex) : hex;
return hex;
}
public static void main(String[] args)
{
int[] nos = {5, 10, 15, 20, 25, 30};
System.out.println("I/P");
Arrays.stream(nos).forEach(System.out::println);
System.out.println("O/P");
Arrays.stream(nos).forEach(i ->
{
System.out.println(decimal2hex(i));
});
}
以上程序的输出
I/P
5
10
15
20
25
30
O/P
05
0A
0F
14
19
1E
使用Integer.toHexString and String.format
public class Main {
public static void main(String[] args) {
// Test hexadecimal representation of integers from 0 to 15
for (int i = 0; i < 16; i++) {
System.out.print(decimal2Hex(i) + " ");
}
}
public static String decimal2Hex(int d) {
return String.format("%2s", Integer.toHexString(d)).toUpperCase().replace(' ', '0');
}
}
输出:
00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
接受的答案工作正常,但仍然太复杂。
您只需要一个简单的 String.format
调用:
public static String decimal2Hex(int d) {
return String.format("%02X", d);
}
0
表示前导零。
2
表示至少两位数字(然后用前导零填充)。
X
表示带大写字母的十六进制(x
将使用小写字母)。
有关格式化选项的完整参考,请参阅 https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Formatter.html#syntax
我的问题是关于 Java。
如何使用 Java 7 将 Java 中的十进制(基数 10)转换为十六进制(基数 16)?
当我在 C# 中使用方法 Convert.FromBase64String(str) with String "BQoPFBke" 我得到结果:05-0A-0F-14-19-1E
但是当我在 Java 中使用具有相同字符串的方法 Base64.Decoder.decode(str) 时,我得到了结果:[5, 10, 15, 20, 25, 30]
我尝试将十进制转换为十六进制:
public static String decimal2hex(int d) {
String digits = "0123456789ABCDEF";
if (d <= 0) return "0";
int base = 16; // flexible to change in any base under 16
String hex = "";
while (d > 0) {
int digit = d % base; // rightmost digit
hex = digits.charAt(digit) + hex; // string concatenation
d = d / base;
}
return hex;
}
但是当我使用例如 decima2hex(15) 方法时 returns 只有:F。但是我需要得到:0F.
如何实现?
请试试这个。
十六进制 = hex.length() <= 1 ? String.format("0%s",十六进制) : 十六进制;
输入输出完成程序
public class App
{
public static String decimal2hex(int d)
{
String digits = "0123456789ABCDEF";
if (d <= 0) return "0";
int base = 16; // flexible to change in any base under 16
String hex = "";
while (d > 0)
{
int digit = d % base; // rightmost digit
hex = digits.charAt(digit) + hex; // string concatenation
d = d / base;
}
hex = hex.length() <= 1 ? String.format("0%s", hex) : hex;
return hex;
}
public static void main(String[] args)
{
int[] nos = {5, 10, 15, 20, 25, 30};
System.out.println("I/P");
Arrays.stream(nos).forEach(System.out::println);
System.out.println("O/P");
Arrays.stream(nos).forEach(i ->
{
System.out.println(decimal2hex(i));
});
}
以上程序的输出
I/P
5
10
15
20
25
30
O/P
05
0A
0F
14
19
1E
使用Integer.toHexString and String.format
public class Main {
public static void main(String[] args) {
// Test hexadecimal representation of integers from 0 to 15
for (int i = 0; i < 16; i++) {
System.out.print(decimal2Hex(i) + " ");
}
}
public static String decimal2Hex(int d) {
return String.format("%2s", Integer.toHexString(d)).toUpperCase().replace(' ', '0');
}
}
输出:
00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
接受的答案工作正常,但仍然太复杂。
您只需要一个简单的 String.format
调用:
public static String decimal2Hex(int d) {
return String.format("%02X", d);
}
0
表示前导零。
2
表示至少两位数字(然后用前导零填充)。
X
表示带大写字母的十六进制(x
将使用小写字母)。
有关格式化选项的完整参考,请参阅 https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Formatter.html#syntax