Originally Posted by
stylelink
what i was thinking is just writing a class separately, which would take a number from 10 to 15 and return its associated letter, here from A to F.
The approach is faulty. Hexadecimal is just a way of expressing a number in a different base. It's incorrect to look at a character and return what that character would be in decimal. You need to an algorithm to convert bases.
Originally Posted by
styelink
But i have no idea how to call it and make it work. If the class name was "Asso", how would i make it work in my converter? would i call it like:
" Asso(y) " for y the variable that i inputed? or another way?
Asso(y) would be the constructor for the object. You would create a member variable and assign it to the value of y. Then you would have a method of Asso that returns the converted result.
public class Asso {
private String _hex;
public Asso(String hexValue) {
_hex = hexValue; //Assign the member variable
}
public int convertToDecimal() {
// Convert base 16 to base 10
return result;
}
}
Then when you want to do a conversion
Asso asso = new Asso("4E2A");
int result = asso.convertToDecimal();
// Check the result against the one provided with Integer.parse
if (result == (Integer.parse("4E2A", 16)) {
System.out.println("My algorithm is correct");
}
Originally Posted by
stylelink
PS: My goal here is to create the converter, in the link you sent, there already is a converter, but i would rather not read it until i am finished (also i'm pretty sure it will be way cleaner/efficient than the one i am working on).
There's nothing wrong with implementing your own converter for a learning exercise but it's a good idea to use the
parse method to verify your results.