Thread: decimal to hexadecimal

  1. #1
    Registered User
    Join Date
    Sep 2008
    Location
    Jakarta
    Posts
    18

    decimal to hexadecimal

    I already understand how to convert it into it's numerical value, but i dont understand how to display the result into it's alphabetical value.
    like the hexadecimal value of 26 = 1A.
    i'm stuck at displaying the value as 26 = 110.

    this is how i do so far :

    can someone please point out my mistake ?

    Code:
    #include <stdio.h>
    
    int main ()
    {
    
    	int ask = 26;
    	int a;
    	int b[9];
    	int c = 0;
    	
    	while ( ask > 0 ) {
    	
    		a = ask % 16;
    		if ( a == 10 ) {
    			
    			b[c] = 'A';
    			}
    		
    		if ( a == 11 ) {
    		
    			b[c] = 'B';
    			}
    			
    		if ( a == 12 ) {
    		
    			b[c] = 'C';
    			}
    		
    		if ( a == 13 ) {
    		
    			b[c] ='D';
    			}
    			
    		if ( a == 14 ) {
    		
    			b[c] = 'E';
    			}
    		
    		if ( a == 15 ) {
    			
    			b[c] = 'F';
    			}
    			else {
    				
    				b[c] = a;
    				}
    		
    		c++;
    		ask = ask / 16;
    		
    		
    		}
    		
    		while ( c ){
    		
    		printf ("%d", b[--c]);
    		
    		}
    		getchar ();
        return 0;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    All your good work gets undone by the last else -- that else is only on the if a==15; if you've already set b[c] to 'A', well, too bad.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Jakarta
    Posts
    18
    i try to replace that last else with

    Code:
    if ( a < 10 ) {
    
       b[c] = a;
       }
    the result was still wrong. it end up displaying 165. any other suggestion ?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by prominababy View Post
    i try to replace that last else with

    Code:
    if ( a < 10 ) {
    
       b[c] = a;
       }
    the result was still wrong. it end up displaying 165. any other suggestion ?
    Well, that's what you wanted, wasn't it? 'A' is 65, after all.[0] If you want letters, why would you be printing ints with &#37;d?

    [0]Obviously only on ASCII machines, but that's apparently what we're on.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Decimal to hexadecimal conversion
    By Nathalie in forum C Programming
    Replies: 9
    Last Post: 12-11-2007, 03:29 PM
  2. would you explain to me? hexadecimal to decimal
    By shteo83 in forum C Programming
    Replies: 2
    Last Post: 02-25-2006, 03:55 PM
  3. stacks to convert from decimal to hexadecimal
    By drdodirty2002 in forum C++ Programming
    Replies: 3
    Last Post: 09-26-2004, 12:24 AM
  4. All u wanted to know about data types&more
    By SAMSAM in forum Windows Programming
    Replies: 6
    Last Post: 03-11-2003, 03:22 PM
  5. decimal to binary, decimal to hexadecimal and vice versa
    By Unregistered in forum C++ Programming
    Replies: 9
    Last Post: 12-08-2001, 11:07 PM