/**
* @(#)dayau_0.java
*
* dayau_0 application
*
* @author
* @version 1.00 2009/9/28
*/

public class dayau_0 {

public static void main(String[] args) {

int num = 30;
String AnsHex = "";
AnsHex = DectoHexDec(num);
System.out.println(AnsHex);
}

public static int DectoHexDec(int x) {

String result = "";
int remainder = 0;
while (x>0){
remainder = x%16;
switch(remainder){
case 1 : result = Integer.toString(remainder)+result;
break;
case 2 : result = Integer.toString(remainder)+result;
break;
case 3 : result = Integer.toString(remainder)+result;
break;
case 4 : result = Integer.toString(remainder)+result;
break;
case 5 : result = Integer.toString(remainder)+result;
break;
case 6 : result = Integer.toString(remainder)+result;
break;
case 7 : result = Integer.toString(remainder)+result;
break;
case 8 : result = Integer.toString(remainder)+result;
break;
case 9 : result = Integer.toString(remainder)+result;
break;
case 10 : result = "A"+result;
break;
case 11 : result = "B"+result;
break;
case 12 : result = "C"+result;
break;
case 13 : result = "D"+result;
break;
case 14 : result = "E"+result;
break;
case 15 : result = "F"+result;
break;
}
x = x/16;
}
return result;
}
}

/**
 * @(#)dayau_0.java
 *
 * dayau_0 application
 *
 * @author 
 * @version 1.00 2009/9/28
 */
 
public class dayau_0 {
    
    public static void main(String[] args) {
    	
    int num = 30;
    String AnsHex = "";
    AnsHex = DectoHexDec(num);
    System.out.println(AnsHex);
    }
    
    public static int DectoHexDec(int x) {
    
    String result = "";
    int remainder = 0;
    while (x>0){
    	remainder = x%16;
    	switch(remainder){
    		case 1 : result = Integer.toString(remainder)+result;
 			break;   
    		case 2 : result = Integer.toString(remainder)+result;
 			break;
    		case 3 : result = Integer.toString(remainder)+result;
 			break; 			
    		case 4 : result = Integer.toString(remainder)+result;
 			break;
    		case 5 : result = Integer.toString(remainder)+result;
 			break; 			
 			case 6 : result = Integer.toString(remainder)+result;
 			break;	
    		case 7 : result = Integer.toString(remainder)+result;
 			break; 				
    		case 8 : result = Integer.toString(remainder)+result;
 			break; 				
    		case 9 : result = Integer.toString(remainder)+result;
 			break;
    		case 10 : result = "A"+result;
 			break;
    		case 11 : result = "B"+result;
 			break;
    		case 12 : result = "C"+result;
 			break; 			
    		case 13 : result = "D"+result;
 			break; 			 			 			 				
    		case 14 : result = "E"+result;
 			break; 			
    		case 15 : result = "F"+result;
 			break; 			
 				}
 		x = x/16;
 		}
 	return result;
    }
}

Why the lengthy switch rather than something like

result = "023456789ABCDEF".charAt(remainder) + result;

Edited 7 Years Ago by JamesCherrill: n/a

Comments
Ha, that was funny. Not to mention that he posted his code twice

If you rplace the ludicrous switch with the single line
result = "023456789ABCDEF".charAt(remainder) + result;
then you won't have that problem anyway.

Theres one problem with this program, the DectoHexDec method header is "public static int DectoHexDec(int x)", but then proceeds to return a string, so that needs to be fixed.

@doomsday1216 and what would be suitable fix according to you? I'm interested to hear about it.

just change int to String (e.g. instead of public static int DectoHexDec(int x), just make it public static String DecToHexDec(int x) )

Why the lengthy switch rather than something like

result = "023456789ABCDEF".charAt(remainder) + result;

In theory, .charAt returns a char, and result is a string. this can be fixed by changing result to an array of chars, and then put a small for loop to put each character into a string. This may seem convoluted, but it's still faster than the massive case/switch block. The for loop would look something like this.

String done="";
		for(int j=0;j<result.length;j++)
		{
			done=result[j]+done;
		}