All pastes #538220 Raw Edit

zahlensysteme

public java v1 · immutable
·published 2007-06-04 23:50 UTC
rendered paste body
/** * Dominik Kriegl 	21291808	Gruppe 10 * Felix Quandt		21301569	? */public class Zahlensysteme {	public static String dezimalZuBinaer(int zahl) {			return (zahl < 1) ? "" : (dezimalZuBinaer(zahl / 2)) + zahl % 2;	}		public static String dezimalZuOktal(int zahl) {		return (zahl < 1) ? "" : (dezimalZuOktal(zahl / 8)) + zahl % 8;	}		public static String dezimalZuHexadezimal(int zahl) {		return (zahl < 1) ? "" : (dezimalZuHexadezimal(zahl / 16)) + hexen(zahl % 16);	}		public static int binaer(String zahl) {		int ziffern[] = new int[zahl.length()];				for (int i = 0; i < zahl.length(); i++) {			ziffern[i] = zahl.charAt(i) - 48;			System.out.println(ziffern[i]);		}		return horner(ziffern, 0, 0, 2);	}	public static int oktal(String zahl) {		int ziffern[] = new int[zahl.length()];				for (int i = 0; i < zahl.length(); i++) {			ziffern[i] = zahl.charAt(i) - 48;		}				return horner(ziffern, 0, 0, 8);	}		public static int hexadezimal(String zahl) {		int ziffern[] = new int[zahl.length()];                zahl = zahl.toUpperCase();				for (int i = 0; i < zahl.length(); i++) {			ziffern[i] = unhex(zahl.charAt(i));		}				return horner(ziffern, 0, 0, 16);	}		public static String hexen(int hx) {  		switch(hx) {  		case 10: return "A"; 		case 11: return "B";		case 12: return "C";		case 13: return "D";		case 14: return "E";		case 15: return "F";		}  		return "" + hx;  	}		public static int unhex(char hx) {		switch(hx) {		case 'A': return 10;		case 'B': return 11;		case 'C': return 12;		case 'D': return 13;		case 'E': return 14;		case 'F': return 15;		}		return hx - 48;	}		public static int horner(int[] ziffern,int zwischenwert, int index, int basis) {		System.out.println(ziffern.length + " :: " + index + " :: " + zwischenwert);		return (index == ziffern.length - 1) ? zwischenwert+ziffern[ziffern.length - 1] : horner(ziffern, (zwischenwert+ziffern[index])*basis, index + 1, basis);	}		public static void main(String[] args) {		System.out.println(dezimalZuBinaer(128));		System.out.println(dezimalZuOktal(87));		System.out.println(dezimalZuHexadezimal(116874));				System.out.println(binaer("1011011"));		System.out.println(oktal("127"));		System.out.println(hexadezimal("1C88A"));	}}