I have a homework assignment where I need to do three-way conversion between decimal, binary and hexadecimal. The function I need help with is converting a decimal into a hexadecimal. I have nearly no understanding of hexadecimal, nonetheless how to convert a decimal into hex. I need a function that takes in an int dec
and returns a String hex
. Unfortunately I don't have any draft of this function, I'm completely lost. All I have is this.
public static String decToHex(int dec)
{
String hex = "";
return hex;
}
Also I can't use those premade functions like Integer.toHexString() or anything, I need to actually make the algorithm or I wouldn't have learned anything.
Simple:
As mentioned here: Java Convert integer to hex integer
A better solution to convert Decimal To HexaDecimal and this one is less complex
One possible solution:
Output:
Anyway, there is a library method for this:
The following converts decimal to Hexa Decimal with Time Complexity : O(n) Linear Time with out any java inbuilt function
Code to convert DECIMAL -to-> BINARY, OCTAL, HEXADECIMAL
I will use
since there is some hex that can be larguer than intenger and it will throw an exception