decimal to hexadecimal

dayau89 0 Tallied Votes 282 Views Share

/**
* @(#)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;
    }
}
masijade 1,351 Industrious Poster Team Colleague Featured Poster

Do you have a question?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Why the lengthy switch rather than something like

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

javaAddict commented: Ha, that was funny. Not to mention that he posted his code twice +9
daredevilnitte 0 Newbie Poster

won't work for number with remainder zero...so insert case 0....

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

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

doomsday1216 0 Newbie Poster

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.

peter_budo 2,532 Code tags enforcer Team Colleague Featured Poster

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

doomsday1216 0 Newbie Poster

@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) )

doomsday1216 0 Newbie Poster

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;
		}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.