Zitat:
Zitat von M.G.
Hallo,
ich suche ein Programm, dass Hexadezimalzahlen und Dezimalzahlen umwandeln kann.
Es sollte aber ein Programm sein, dass es per Hand macht und nicht den einzeiligen Standardbefehl.
Würde gerne anhand dieses Beispiels verstehen, wie man sowas in Java überträgt. Ein Beispiel dazu wäre:
Umlaut ä als Unicode: 00E4
Hexadezimal → Dezimal: E4(16) = 14·16^1+4·16^0 = 228(10) // die () bezeichnen den Index
(wobei E = 14)
Umgekehrt:
Dezimal → Hexadezimal: 228 : 16 = 14 Rest 14 (= E)
4 : 16 = 0 Rest 4
228(10) = E4(16)
|
Ich habe bisher folgendes, aber könnte man es nicht irgendwie verkürzen?
Code:
import java.util.Scanner;
public class hexazahlen {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner tastatur; //Scanner
tastatur = new Scanner(System.in);
int dezimale;
System.out.print("Bitte geben sie eine Zahl von 0-255 ein: ");
dezimale=tastatur.nextInt();
int restx = dezimale/16; //Berechnung der Hexazahl
int reste = dezimale%16; //Berechnung der Hexazahl
if(dezimale <10){
System.out.print("Ihre Zahl ist im Hexadezimalsystem: " +dezimale);
}
if(dezimale >9 & dezimale <16){
switch (dezimale)
{
case 10:
System.out.print("Ihre Zahl ist als Hexadezimale: A");
break;
case 11:
System.out.print("Ihre Zahl ist als Hexadezimale: B");
break;
case 12:
System.out.print("Ihre Zahl ist als Hexadezimale: C");
break;
case 13:
System.out.print("Ihre Zahl ist als Hexadezimale: D");
break;
case 14:
System.out.print("Ihre Zahl ist als Hexadezimale: E");
break;
case 15:
System.out.print("Ihre Zahl ist als Hexadezimale: F");
}
}
if(dezimale >15){
if(restx >9 & restx <16) {
switch (restx)
{
case 10:
System.out.print("Ihre Zahl ist als Hexadezimale: A");
break;
case 11:
System.out.print("Ihre Zahl ist als Hexadezimale: B");
break;
case 12:
System.out.print("Ihre Zahl ist als Hexadezimale: C");
break;
case 13:
System.out.print("Ihre Zahl ist als Hexadezimale: D");
break;
case 14:
System.out.print("Ihre Zahl ist als Hexadezimale: E");
break;
case 15:
System.out.print("Ihre Zahl ist als Hexadezimale: F");
}
}
else{
System.out.print("Ihre Zahl ist im hexadezimalsystem: "+restx);
}
if(reste >9 & reste <16){
switch (reste)
{
case 10:
System.out.print("A");
break;
case 11:
System.out.print("B");
break;
case 12:
System.out.print("C");
break;
case 13:
System.out.print("D");
break;
case 14:
System.out.print("E");
break;
case 15:
System.out.print("F");
}
}
else{
System.out.print(+reste);
}
}
}
}
kann man das nicht irgendwie kürzer fassen?
sorry, falscher button.