从缓冲读取器的十六进制到十进制的转换

Hex to decimal conversion from a bufferreader

我很难让我的代码读取某个文件,该文件有 4 个十六进制代码,使用缓冲区 reader 并将其转换为十进制。我能够让缓冲区 reader 读取文本文件并将其输出到编译器,但我需要程序来存储 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,然后代码看起来像上面从文件读取并打印到输出流。 希望对你有帮助。

正如 Leonid Glanz 所说,只需使用 Integer.parseInt(hexString, 16) 将十六进制字符串转换为十进制。要存储多个值,只需使用 ArrayList。

编辑:Sry 没有正确阅读您的 Op

这里有一些您可以使用的代码:

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

public class HexToDecimal {

    public static ArrayList<Integer> hexToDecimal() throws IOException {
        FileReader in = new FileReader("results.txt");
        BufferedReader br = new BufferedReader(in);
        String line;
        String ram = "";
        ArrayList<String> lines = new ArrayList<>();
        ArrayList<Integer> output = new ArrayList<>();
        while ((line = br.readLine()) != null) {
             lines.add(line); 
        }
        for(String y : lines){
            ram = "";
            for(char x : y.toCharArray()){
                ram += convert(x);
            }
            output.add(Integer.parseInt(ram));
        }

        in.close();
        return output;
    }

    public static int convert(char x){
        if(x == '1'){
            return 1;
        }else if(x == '2'){
            return 2;
        }else if(x == '3'){
            return 3;
        }else if(x == '4'){
            return 4;
        }else if(x == '5'){
            return 5;
        }else if(x == '6'){
            return 6;
        }else if(x == '7'){
            return 7;
        }else if(x == '8'){
            return 8;
        }else if(x == '9'){
            return 9;
        }else if(x == 'A'){
            return 10;
        }else if(x == 'B'){
            return 11;
        }else if(x == 'C'){
            return 12;
        }else if(x == 'D'){
            return 13;
        }else if(x == 'E'){
            return 14;
        }else if(x == 'F'){
            return 15;
        }else{
            return 0;
        }
    }

    public static void main(String[] args) { 
        try {
            System.out.println(hexToDecimal());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    } 
}