PDA

Click to See Complete Forum and Search --> : math class


quipy
May 23rd, 2003, 09:53 PM
is there some method in the math class (or any class for that matter) that could calculate hexadecimal inputs?

Dillinger4
May 24th, 2003, 12:36 PM
If you already know what the Hex input will look like you can just pass that as a String to the Byte.parseByte(String s, int radix) method. If you are working with integers and you are not sure of how to calculate numbering systems then you can just use the Integer.toHexString(int i) method.


public class Test3{
public static void main(String[] args){

int i = 30;
String hexstring = Integer.toHexString(i);
System.out.println(hexstring);

byte b = Byte.parseByte(hexstring,16);
System.out.println(b);
}
}

Dillinger4
May 24th, 2003, 01:19 PM
If you mean "calculating" by adding or doing some other mathematical procedure then you will probably have to go about that manually. I dont think that there are any functions in the API that take Hex values as arguements and calculates them.

public class Test3{
public static void main(String[] args){

int i = 330;
int k = 70;

String hex1 = Integer.toHexString(i);
String hex2 = Integer.toHexString(k);
long l = calHex(hex1, hex2);
System.out.println(l);
}

public static long calHex(String hex1, String hex2){
// byte b = Byte.parseByte(hex1,16); only values under 128!
int i = Integer.parseInt(hex1,16);
int k = Integer.parseInt(hex2,16);
return i + k;
}
}

quipy
May 24th, 2003, 09:22 PM
thanks!