Hallo alle zusammen,
ich habe folgenden Code, der eine duale zahl in dezimal umrechnen soll. Das Programm gibt bei binärzahl 1101 -->733 und nicht 13 aus:
Ich denke die rest-Berechnung ist nicht ganz korrekt. Aber mit typecast (int) davor hats nicht geklappt.
Vielen Dank für eure Hilfe!
Gruß
ich habe folgenden Code, der eine duale zahl in dezimal umrechnen soll. Das Programm gibt bei binärzahl 1101 -->733 und nicht 13 aus:
Java:
public class DuallInDez {
public static void main(String[]args)
{
String dual = "1101";
int rest;
int zahl=0;
int potenzZahl =1;
for(int i = dual.length()-1;i>=0;i--)
{
rest = dual.charAt(i)*potenzZahl;
zahl = rest + zahl;
potenzZahl = potenzZahl*2;
}
System.out.println(zahl);
}
}
Ich denke die rest-Berechnung ist nicht ganz korrekt. Aber mit typecast (int) davor hats nicht geklappt.
Vielen Dank für eure Hilfe!
Gruß
Zuletzt bearbeitet von einem Moderator: