/*
* Created on 14.01.2004
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
/**
* @author Administrator
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public class HexToInt {
public static void main(String[] args) {
HexToInt hti = new HexToInt();
System.out.print(""Bitte geben sie eine 8-stellige Hexadezimalzahl ein: ");
System.out.println(hti.doIt(args[0]));
System.out.println(hti.doIt2(args[0]));
}
/**
*
*/
private long doIt(String hexStr) {
// TODO Auto-generated method stub
if (hexStr == null || hexStr.length() == 0 || hexStr.length() > 8) {
System.out.println(
"Sie haben entweder keine Zahl angegeben oder diese ist zu lang!");
return -1l;
}
char[] cArray = hexStr.toCharArray();
long value = 0l;
int pos = cArray.length - 1;
for (int i = pos; i >= 0; i--) {
switch ((int) cArray[i]) {
case (int) '0' :
value += 0;
break;
case (int) '1' :
value += (((int) '1' - (int) '0') * Math.pow(16, pos - i));
break;
case (int) '2' :
value += (((int) '2' - (int) '0') * Math.pow(16, pos - i));
break;
case (int) '3' :
value += (((int) '3' - (int) '0') * Math.pow(16, pos - i));
break;
case (int) '4' :
value += (((int) '4' - (int) '0') * Math.pow(16, pos - i));
break;
case (int) '5' :
value += (((int) '5' - (int) '0') * Math.pow(16, pos - i));
break;
case (int) '6' :
value += (((int) '6' - (int) '0') * Math.pow(16, pos - i));
break;
case (int) '7' :
value += (((int) '7' - (int) '0') * Math.pow(16, pos - i));
break;
case (int) '8' :
value += (((int) '8' - (int) '0') * Math.pow(16, pos - i));
break;
case (int) '9' :
value += (((int) '9' - (int) '0') * Math.pow(16, pos - i));
break;
case (int) 'A' :
value
+= (((10 + (int) 'A' - (int) 'A'))
* Math.pow(16, pos - i));
break;
case (int) 'B' :
value
+= (((10 + (int) 'B' - (int) 'A'))
* Math.pow(16, pos - i));
break;
case (int) 'C' :
value
+= (((10 + (int) 'C' - (int) 'A'))
* Math.pow(16, pos - i));
break;
case (int) 'D' :
value
+= (((10 + (int) 'D' - (int) 'A'))
* Math.pow(16, pos - i));
break;
case (int) 'E' :
value
+= (((10 + (int) 'E' - (int) 'A'))
* Math.pow(16, pos - i));
break;
case (int) 'F' :
value
+= (((10 + (int) 'F' - (int) 'A'))
* Math.pow(16, pos - i));
break;
default :
{
System.out.println("Ungültiges Zeichen: " + cArray[i]);
return -1;
}
}
}
return value;
}
public long doIt2(String hexStr) {
return Long.parseLong(hexStr, 16);
}
}