okay, what we are going to do is to create an array of string or character.
and that array will consist of 0-9, A-F.
but make sure that you use a uniform case for letters, if A is in uppercase, the rest must be in uppercase also.
something like this
char[] hex = {'0','1','2'......'F'}
as you noticed
char 0 is at
index 0,
char A is at
index 10,
char F is at
index 15. did you see the pattern/logic here?
what we are going to do is get their index, so
char B is equal to 11 right?
that way we have now a hex to binary converter (single digit)
how are going get the index of char hex?
well you can do some algorithm.
but the easiest way to get it is using the
binarySearch method of
Arrays class in java
kindly visit this link and see how
binarySearch works for arrays
Arrays (Java Platform SE 8 )
--------------
actually we can do it without using array.
we can do the convertion just by getting its ASCII.
but for now, we are going to start with basics.
--------------
If the hexadecimal number is a string you could use the index of each character for your math and as you loop over all the characters you can use a switch statement with 16 cases so you never have to convert anything.
yeah we can use switch cases here.
but it will be easier to use the
binarySearch as what I said in the above statement.
the advantage is it will make the code
shorter and will help the programmer learn/imporve on how to construct an algorithm. 16 switch cases is too long codes just to convert hex to decimal.
And how do I get it to read whatever the input puts in from right to left? Thanks again!
do you know how to get characters from left? using loop? do you know how to use
toCharArray method of String? do you know how to create a loop?
actually it is not necessary to read from right to left, we can also do the conversion from left to rigth, but reading it from left will make us add another statement that will compute on what number should the exponent starts. as you noticed, reading it from right will make our exponent equal to 0