954,545 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?

decimal to hexadecimal

0
By dhiyauddin on Sep 28th, 2009 11:40 am

/**
* @(#)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;
    }
}

Do you have a question?

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

Why the lengthy switch rather than something like

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

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

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

daredevilnitte
Newbie Poster
1 post since Oct 2009
Reputation Points: 10
Solved Threads: 0
 

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

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

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
Newbie Poster
7 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
 

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

peter_budo
Code tags enforcer
Moderator
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
 
@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
Newbie Poster
7 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
 

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;
		}
doomsday1216
Newbie Poster
7 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You