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

时间:2015-10-01 06:41:32

标签: java bufferedreader filereader

我很难让我的代码读取某个文件,该文件有4个十六进制代码,使用缓冲区读取器并将其转换为十进制。我能够让缓冲区读取器读取文本文件并将其输出到编译器,但我需要程序存储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,这就是为什么我使用案例中断。在将十六进制转换为十进制时,这也给我带来了麻烦。任何帮助都非常感谢。

2 个答案:

答案 0 :(得分:0)

你可以用这种方式改变你的代码,它会起作用:

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然后代码看起来像上面的文件读取和打印到输出流。 我希望它有所帮助。

答案 1 :(得分:0)

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

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

以下是您可以使用的一些代码:

    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();
        }
    } 
}