gpt4 book ai didi

java - 从十六进制转换为二进制以及二进制到十六进制,不使用转换,读取txt文件以查找RAM中的错误

转载 作者:行者123 更新时间:2023-12-01 10:23:06 25 4
gpt4 key购买 nike

我正在尝试创建一个 java 程序,该程序将计算机中 RAM 位置中可能存在的错误转换为十六进制。然后从十六进制切换到二进制,再从二进制切换到十进制,以找到损坏的 RAM 的位置。这是我到目前为止的代码:

public class Practice {

private static String result;
private static int j;

public static void hexToBinary() throws IOException {
Scanner infile = new Scanner(new File("RAMerrors.txt"));

result = "";
String binVal; // the binary value of the Hex

while (infile.hasNextLine()) {
String line = infile.nextLine();
Scanner input = new Scanner(line);
String hex = input.next();

for (int i = 0; i < hex.length(); i++) {
char hexChar = hex.charAt(i);

switch (hexChar) {
case ('0'):
binVal = "0000";
break;
case ('1'):
binVal = "0001";
break;
case ('2'):
binVal = "0010";
break;
case ('3'):
binVal = "0011";
break;
case ('4'):
binVal = "0100";
break;
case ('5'):
binVal = "0101";
break;
case ('6'):
binVal = "0110";
break;
case ('7'):
binVal = "0111";
break;
case ('8'):
binVal = "1000";
break;
case ('9'):
binVal = "1001";
break;
case ('A'):
binVal = "1010";
break;
case ('B'):
binVal = "1011";
break;
case ('C'):
binVal = "1100";
break;
case ('D'):
binVal = "1101";
break;
case ('E'):
binVal = "1110";
break;
case ('F'):
binVal = "1111";
break;
default:
binVal = "invalid input";
break;


}
result += binVal;
}
System.out.println("Binary of " + hex + ":" + result);

}

}

public static void binaryToDecimal() {
double j=0;
for(int i=0;i<result.length();i++)
{
if(result.charAt(i)== '1')
{
j= j + Math.pow(2, result.length()-1-i);
}

}
System.out.println("Decimal value: " + j);

}




public static void main(String arg[]) throws IOException {

hexToBinary();
binaryToDecimal();
}

}

十六进制到二进制没问题。小数部分只显示一个数字(而且它不是正确的值)。不太确定使用什么循环或如何将二进制运行到十进制部分。预先感谢您的任何建议! txt 文件具有以下值:

ABCDEFABC
1A00D0000
7A0EDF301
3CDAEFFAD

最佳答案

好吧,你的问题我不清楚。您只得到一个号码的原因是您从 main 调用电话。至hexToBinary ,使用此方法循环遍历整个文件,然后返回到 main 。之后,您调用binaryToDecimal它打印出 result 的值,这是它从 hexToBinary 获得的最后一个值.
您需要做的是从文件中读取一个值,将其转换为二进制,然后转换为十进制,然后继续处理下一个值。一种方法是这样的:
更改您的方法以返回值并获取参数。
hexToBinary应该是 -

public static String hexToBinary(String hex) throws IOException {
String result = "";
String binVal; // the binary value of the Hex
for (int i = 0; i < hex.length(); i++) {
char hexChar = hex.charAt(i);

switch (hexChar) {
case ('0'):
binVal = "0000";
break;
case ('1'):
binVal = "0001";
break;
case ('2'):
binVal = "0010";
break;
case ('3'):
binVal = "0011";
break;
case ('4'):
binVal = "0100";
break;
case ('5'):
binVal = "0101";
break;
case ('6'):
binVal = "0110";
break;
case ('7'):
binVal = "0111";
break;
case ('8'):
binVal = "1000";
break;
case ('9'):
binVal = "1001";
break;
case ('A'):
binVal = "1010";
break;
case ('B'):
binVal = "1011";
break;
case ('C'):
binVal = "1100";
break;
case ('D'):
binVal = "1101";
break;
case ('E'):
binVal = "1110";
break;
case ('F'):
binVal = "1111";
break;
default:
binVal = "invalid input";
break;
}
result += binVal;
}
return result;
}

binaryToDecimal应该是-

public static Double binaryToDecimal(String result) {
double j=0;
for(int i=0;i<result.length();i++)
{
if(result.charAt(i)== '1')
{
j= j + Math.pow(2, result.length()-1-i);
}

}
return j;
}

还有main将读取文件,将值传递给这两个方法并打印结果:

    public static void main(String args[]) throws IOException {

Scanner infile = new Scanner(new File("RAMerrors.txt"));
String result;
Double decimal;

while (infile.hasNextLine()) {
String line = infile.nextLine();
Scanner input = new Scanner(line);
String hex = input.next();
result = hexToBinary(hex);
System.out.println("Binary of " + hex + ":" + result);
decimal = binaryToDecimal(result);
System.out.println("Decimal value: " + decimal);
}

输出为

Binary of ABCDEFABC:101010111100110111101111101010111100 Decimal value: 4.6118402748E10 Binary of 1A00D0000:000110100000000011010000000000000000 Decimal value: 6.980173824E9 Binary of 7A0EDF301:011110100000111011011111001100000001 Decimal value: 3.2764719873E10 Binary of 3CDAEFFAD:001111001101101011101111111110101101 Decimal value: 1.6335699885E10

希望这对您有帮助。

关于java - 从十六进制转换为二进制以及二进制到十六进制,不使用转换,读取txt文件以查找RAM中的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35465854/

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