gpt4 book ai didi

java - 从 bufferedreader 进行十六进制到十进制的转换

转载 作者:太空宇宙 更新时间:2023-11-04 13:23:09 24 4
gpt4 key购买 nike

我在让我的代码读取某个文件时遇到了很多麻烦,该文件有 4 个十六进制代码,使用缓冲区读取器并将其转换为十进制。我能够让 bufferreader 读取文本文件并将其输出到编译器,但我需要程序来存储 4 个值并让我的方法将十六进制转换为十进制。这是我到目前为止所拥有的:

import java.io.BufferedReader;
import static java.lang.System.*;
import java.io.*;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class HexToDecimal {

public static int hexToDecimal(String hexInput) throws IOException {
int decimal = 0;
int len = hexInput.length();

FileReader in = new FileReader("results.txt");
BufferedReader br = new BufferedReader(in);

String line;
while ((line = br.readLine()) != null) {
out.printf(line + "\n");
}
in.close();


for (int i = 0; i < len; ++i) {
char c = hexInput.charAt(i);
int cValue;

switch (c) {
case '1':
cValue = 1;
break;
case '2':
cValue = 2;
break;
case '3':
cValue = 3;
break;
case '4':
cValue = 4;
break;
case '5':
cValue = 5;
break;
case '6':
cValue = 6;
break;
case '7':
cValue = 7;
break;
case '8':
cValue = 8;
break;
case '9':
cValue = 9;
break;
case 'A':
cValue = 10;
break;
case 'B':
cValue = 11;
break;
case 'C':
cValue = 12;
break;
case 'D':
cValue = 13;
break;
case 'E':
cValue = 14;
break;
case 'F':
cValue = 15;
break;
default: // unexpected character
throw new IllegalArgumentException("Non-hex character " + c
+ " found at position " + i);
}
decimal = 16 * decimal + cValue;
}
return decimal;

}

public static void main(String[] args) {

}

}

此外,我无法使用 parseInt,这就是我使用大小写分隔符的原因。在将十六进制转换为十进制时,这也给我带来了麻烦。非常感谢任何帮助。

最佳答案

您可以通过这种方式更改代码,它会起作用:

import static java.lang.System.out;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.math.BigInteger;

public class HexToDecimal {

public static BigInteger hexToDecimal(String hexInput) throws IOException {
BigInteger decimal = BigInteger.ZERO;
int len = hexInput.length();

for (int i = len - 1; i >= 0; i--) {
char c = hexInput.charAt(len - i - 1);
BigInteger cValue;

switch (c) {
case '1':
cValue = BigInteger.ONE;
break;
case '2':
cValue = BigInteger.valueOf(2l);
break;
case '3':
cValue = BigInteger.valueOf(3l);
break;
case '4':
cValue = BigInteger.valueOf(4l);
break;
case '5':
cValue = BigInteger.valueOf(5l);
break;
case '6':
cValue = BigInteger.valueOf(6l);
break;
case '7':
cValue = BigInteger.valueOf(7l);
break;
case '8':
cValue = BigInteger.valueOf(8l);
break;
case '9':
cValue = BigInteger.valueOf(9l);
break;
case 'A':
cValue = BigInteger.valueOf(10l);
break;
case 'B':
cValue = BigInteger.valueOf(11l);
break;
case 'C':
cValue = BigInteger.valueOf(12l);
break;
case 'D':
cValue = BigInteger.valueOf(13l);
break;
case 'E':
cValue = BigInteger.valueOf(14l);
break;
case 'F':
cValue = BigInteger.valueOf(15l);
break;
default: // unexpected character
throw new IllegalArgumentException("Non-hex character " + c
+ " found at position " + i);
}
decimal = decimal.add(cValue
.multiply(BigInteger.valueOf(16).pow(i)));
}
return decimal;

}

public static void main(String[] args) throws IOException {
FileReader in = new FileReader("results.txt");
BufferedReader br = new BufferedReader(in);

String line;
while ((line = br.readLine()) != null) {
String[] hexNums = line.split(" ");
for (String hex : hexNums)
out.printf(hexToDecimal(hex) + "\n");
}
in.close();

}

}

如果你必须使用大数字,那么你会得到一个错误的答案,使用 int 作为类型,所以你必须使用 BigInteger,然后代码看起来像上面一样,从文件读取并打印到输出流。我希望它有帮助。

关于java - 从 bufferedreader 进行十六进制到十进制的转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32880986/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com