# How to Convert to Hexadecimal using only while and if-else loops

I have to convert a given number from decimal to hexadecimal using only the loops mentioned in the title. Meaning no for loops. So far everything works like converting 683. It prints 2AB no problem, and 34821 prints 8805. The issue is when there needs to be an integer in between two digits. For example I am unable to convert 31388 to 7A9C. I have an idea of what my issue is but I dont know how to fix it.

``````import java.util.Scanner;
public class Lab12abHexNumbersTEST
{
public static void main(String[] args)
{
Scanner decimal = new Scanner(System.in);
System.out.println("Enter a decimal number between 1 and 50,000: ");
int num = decimal.nextInt();

String hex1 = "";
String letter = "";
String hex3 = "";
String hex_last = "";
int dec = num;
while (dec > 0)
{
hex1 = (dec % 16) + hex1;

if(dec % 16 >= 9){
hex1 = "";

if(dec % 16 == 15) {
letter = "F" + letter + hex1;
} else if (dec % 16 == 14) {
letter = "E" + letter + hex1;
} else if (dec % 16 == 13) {
letter = "D" + letter + hex1;
} else if (dec % 16 == 12) {
letter = "C" + letter + hex1;
} else if (dec % 16 == 11) {
letter = "B" + letter + hex1;
} else if (dec % 16 == 10) {
letter = "A" + letter + hex1;
}

System.out.println(letter);
}
hex3 = hex1 + letter;

dec = dec / 16;

}

hex_last = hex3;
System.out.println("The decimal number " + num + " in hexadecimal is "
+ hex_last);
}
}
``````

## 2 Answers How to Convert to Hexadecimal using only while and if-else loops

I have provided a solution to this problem.

Note: I have used switch statements to make the code cleaner, but I'm sure you know how to convert them back into if/else cases. If you need help, please ask.

Note: I have used a StringBuilder instead of strings directly. You can easily change these to strings, but I have chosen stringbuilder because again, it makes the code cleaner.

``````public static void main(String[] args) {
int dec = 31388 ;
StringBuilder hexString = new StringBuilder();

while (dec != 0){
int hexDigit = dec%16; // obtain this remainder
dec /= 16; //update dec

if (hexDigit < 10) hexString.insert(0, hexDigit);
else {
switch (hexDigit) {
case 10 :
hexString.insert(0, "A");
break;
case 11 :
hexString.insert(0, "B");
break;
case 12 :
hexString.insert(0, "C");
break;
case 13 :
hexString.insert(0, "D");
break;
case 14 :
hexString.insert(0, "E");
break;
case 15 :
hexString.insert(0, "F");
break;
default: break;
}
}
} // while
System.out.println(hexString);
}
``````

Output: `7A9C`

converted to an if/else block on request:

``````else {
if (hexDigit == 10) {
hexString.insert(0, "A");
} else if (hexDigit == 11) {
hexString.insert(0, "B");
} else if (hexDigit == 12) {
hexString.insert(0, "C");
} else if (hexDigit == 13) {
hexString.insert(0, "D");
} else if (hexDigit == 14) {
hexString.insert(0, "E");
} else if (hexDigit == 15) {
hexString.insert(0, "F");
}
}
``````

Using strings, the solution is:

``````public static void main(String[] args) {
int dec = 31388;
String answer = "";

while (dec != 0){
int hexDigit = dec%16; // obtain this remainder
dec /= 16; //update dec

if (hexDigit < 10) {
answer = hexDigit + answer;
}
else {
if (hexDigit == 10) {
answer = "A" + answer;
} else if (hexDigit == 11) {
answer = "B" + answer;
} else if (hexDigit == 12) {
answer = "C" + answer;
} else if (hexDigit == 13) {
answer = "D" + answer;
} else if (hexDigit == 14) {
answer = "E" + answer;
} else if (hexDigit == 15) {
answer = "F" + answer;
}
}
} // while
System.out.println(answer);
}
``````

### PhaseRush3 months ago

If String functions are allowed:

``````import java.util.*;

public static void main(String[] args){
int dec = 31388 ;
String digits = "0123456789ABCEDF";
StringBuilder hexString = new StringBuilder();

while(dec != 0){
hexString.insert(0, digits.indexOf(dec % 16));
dec /= 16;
}

System.out.println(hexString);
}
``````