Decimal to Hex Converter

This is a discussion on Decimal to Hex Converter within the C Programming forums, part of the General Programming Boards category; hey , im trying to create a dec to hex converter and the same formula works for all other bases ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    84

    Decimal to Hex Converter

    hey , im trying to create a dec to hex converter and the same formula works for all other bases but for hex i cant get it to display the letters 'a' - 'f' , if i use "%c" , i cant get the numbers to work!..please help.

    Code:
    int convertHex(int num)
    {
    	quotient=(num / 16);
    	remainder=(num % 16);
    
    		if (remainder <= 9 && quotient >= 1)
    		{
    			b[j++]=remainder;
    			count++;
    			convertHex(quotient);
    		}
    		
    		else if(remainder > 9 && quotient >= 1)
    		{
    			b[j++]=(((remainder-10)+'A'));
    			count++;
    			convertHex(quotient);
    		}
    		else if(quotient <= 0)
    		{	
    			(remainder <= 9 ) ? (b[j++]=remainder) : (b[j++]=(((remainder-10)+'A')));
    			count++;
    		
    		//reverse(a,count);
    		for (j = 0; j < count; ++j)
    		{
    			printf("%d",b[j]);
    		}
    
    		printf("\n");
    		
    		b[bits];
    		count = 0;
    		for (j = 0; j < count; ++j)
    		{
    			b[j] = 0;
    		}
    		return b[0];
    	}
    }
    
    int *reverse(int *swap,int b)
    {
        int a = 0;
        int temp;
        
        for(a ; a < --b ; a++) 
        {
            temp=swap[a];       
            swap[a]=swap[b];    
            swap[b]=temp;       
        }
        return swap;    
    }
    ive got a function which uses just printf instead of arrays however i cant reverse the digits in that ,
    Code:
    int convertHex(int num)
    {
    	quotient=(num / 16);
    	remainder=(num % 16);
    
    		if (remainder <= 9 && quotient >= 1)
    		{
    			printf("%d",remainder);
    			convertHex(quotient);
    		}
    		
    		else if(remainder > 9 && quotient >= 1)
    		{
    			printf("%c",((remainder-10)+'A'));
    			convertHex(quotient);
    		}
    		else if(quotient <= 0)
    		{	
    			(remainder <= 9 ) ? printf("%d",remainder): printf("%c",((remainder-10)+'A'));
    			printf("\n");
    			return 0;
    		}
    }
    either one is fine , just as long as it works!..thanks !!
    Last edited by rocketman03; 10-25-2008 at 04:05 AM.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Because int x=0x7a really is the same number as int y=122, the only meaningful "hex conversion" that can take place with C integers involves realizing them as a string for some purpose.

    Code:
    #include <stdio.h>
    
    int main() {
    	int x=0x7a, y=122, hex, dec;
    	printf( "Convert hex to dec: %d\n",x);
    	printf( "Convert dec to hex: %x\nNow input a decimal number:",y);
    	scanf("%d",&dec);
    	printf("convert to hex: %x\nInput a hexadecimal number:",dec);
    	scanf("%x",&hex);
    	printf("convert to dec: %d\n",hex);
    }
    I hope this simplifies things for you.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hex String to Decimal converter
    By wrex in forum C Programming
    Replies: 16
    Last Post: 11-05-2008, 06:06 PM
  2. Replies: 11
    Last Post: 03-24-2006, 11:26 AM
  3. hex to binary,hex to decimal
    By groovy in forum C Programming
    Replies: 2
    Last Post: 01-25-2006, 02:14 AM
  4. Converting decimal to hex
    By cobrakidd in forum C++ Programming
    Replies: 9
    Last Post: 02-06-2003, 11:37 AM
  5. about decimal to hex progam
    By Abdi in forum C Programming
    Replies: 2
    Last Post: 06-01-2002, 08:08 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21