Ich habe versucht, an einem Projekt für Stunden jetzt zu arbeiten, und ich kann nicht herausfinden, wie eine Datei von Hexadezimalzahlen in Dezimalzahlen mit a konvertieren Schalter. Es kommt immer wieder mit Zahlen wie folgt: Dieses Programm konvertiert Hexadezimalzahlen in Dezimalzahlen. Der hexadezimale 2102: -2 ganzen Weg dazu: Der hexadezimale 124F ist: 9342467183 Hier ist der Code, den ich bisher habe:Kann nicht von Hexadezimal von einer Datei in Dezimalform mit einem Schalter konvertieren
import java.io.* ;
import java.util.Scanner ;
public class hexToDecimal {
public static void main(String[] args) throws Exception {
java.io.File in_file = new java.io.File("hex_data.txt");
Scanner fnput = new Scanner(in_file);
String[] hexL = new String[25] ;
int[] dec = new int [25] ;
long decNum = 0 ;
System.out.printf("This program converts hexadecimal numbers into decimal numbers. \n");
for(int a = 0 ; a < 25 ; a++) {
String hex = fnput.nextLine();
hexL[a] = hex.substring(2).toUpperCase() ;
hexL[a] = hexL[a].trim();
int powerNum = hexL.length - a - 1;
dec[a] = hexL[a].charAt(dec[a]);
//dec[a] = pow(16, a) ;
switch(dec[a]){
case '0':decNum = 0 * pow(16, powerNum) + decNum;break;
case '1':decNum = 1 * pow(16,powerNum) + decNum;break;
case '2':decNum = 2 * pow(16,powerNum) + decNum;break;
case '3':decNum = 3 * pow(16,powerNum) + decNum;break;
case '4':decNum = 4 * pow(16,powerNum) + decNum;break;
case '5':decNum = 5 * pow(16,powerNum) + decNum;break;
case '6':decNum = 6 * pow(16,powerNum) + decNum;break;
case '7':decNum = 7 * pow(16,powerNum) + decNum;break;
case '8':decNum = 8 * pow(16,powerNum) + decNum;break;
case '9':decNum = 9 * pow(16,powerNum) + decNum;break;
case 'A':decNum = 10 * pow(16,powerNum)+ decNum;break;
case 'B':decNum = 11 * pow(16,powerNum)+ decNum;break;
case 'C':decNum = 12 * pow(16,powerNum)+ decNum;break;
case 'D':decNum = 13 * pow(16,powerNum)+ decNum;break;
case 'E':decNum = 14 * pow(16,powerNum)+ decNum;break;
case 'F':decNum = 15 * pow(16,powerNum)+ decNum;break;
}
System.out.println("The hexadecimal " + hexL[a] + " is: " + decNum);
}
} // end of function main()
public static int pow(int i, int powerNum) {
return (int) Math.pow(16, powerNum);
}// end of function convertHex()
} // end of class hexToDecimal