954,168 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

convert Decimal to Hexadecimal system..

hi eveRyone,
I just want to have your tips for my code..

#include <stdio.h>

main()
{
hexa = 16;
			printf("enter decimal number: ");

			scanf("%d",&deci);

			for (ctr = 1; ctr<=deci; ctr++)

			quotient = deci / hexa;

                        printf("Equivalent in Hexadecimal is %d",quotient);

			
			getche();
                   }


I want to print out the numbers 10-16 in letters like this, 10=A,11=B,12=C,13=D,14=E,15=F,16=G..but i dont know how to make it coz im new with C..

campuzcrazyness
Newbie Poster
17 posts since Jan 2008
Reputation Points: 6
Solved Threads: 0
 

the easiest way is to use sprintf with "%X" format specifier

int main()
{
    char buf[255] = {0};
    int x = 1234;
    sprintf(buf,"%X", x);
    printf("%s\n", buf);
    return 0;
}
Ancient Dragon
Retired & Loving It
Team Colleague
30,043 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,341
 

wow! thanks a lot mr.,but still nothing happened..

campuzcrazyness
Newbie Poster
17 posts since Jan 2008
Reputation Points: 6
Solved Threads: 0
 

You made me laugh with your post to convert decimal to hex, including "A,B...G"
*G* ?? :D

Anyway, your algorithm to change decimal to hexadecimal is wrong. Here's a modded version of your program, which shows what the hex value of a decimal, should be:

#include <stdio.h>

int main(void)
{
   int hexa = 16, deci, quotient, ctr;
   printf("enter decimal number: ");
   scanf("%d",&deci);

   for (ctr = 1; ctr<=deci; ctr++)
      quotient = deci / hexa;

   printf("Equivalent in Hexadecimal is %d",quotient);
   printf("\n\n Try this for Hexadecimal: %X", deci);   

   getche();
   return 0;
}

Good luck with your studies.
Adak
Nearly a Posting Virtuoso
1,479 posts since Jun 2008
Reputation Points: 425
Solved Threads: 185
 

thanKs duDe!

campuzcrazyness
Newbie Poster
17 posts since Jan 2008
Reputation Points: 6
Solved Threads: 0
 

Not to be nitpicky, but getche() isn't standard C. You should use getchar() instead.

And here's a link about scanf () (or did I give you it before?)

Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 

Thank you very much dude, I appreciate that!

Can you help me with my prob?
here's my code..

#include <stdio.h>


int main(void)  {


   int ctr, bin, quotient, deci=0, binary = 2;
   float rem;
   char mark_magic;

      
     
       
   
         gotoxy(28,9);printf("enter decimal number: ");

         scanf("%d",&deci);
         quotient = deci / binary;

         printf("Equivalent in Binary is ");

	 for(bin=0; bin <=3; bin++)
	 {
	    printf("%d",deci % 2);
            deci = deci / 2;
         }


     
         getchar();
}

I want to print it out in binary, but the answer is being written out in reverse.

campuzcrazyness
Newbie Poster
17 posts since Jan 2008
Reputation Points: 6
Solved Threads: 0
 

Did you learn about arrays yet? You could store everything in an array and then print them out in reverse order.
You could also change the calculation. Look into the pow() function

Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 
void printB( int num )
{
     if( num == 0 )
         return;
     else
     {
         printB( num / 2 );
         printf("%d", num % 2 );
     }
}


You could solve the problem of you binary values printing in reverse using the recursive function. Have a look the function above. You could see the before the function is called again it pushes the "num % 2" to the stack and when it return it print the values which is on top of the static that is "0%2" ........ "num%2".

Call the function as follow:

printB( 23 );

/* my output
10111
*/


NOTE: I wonder this would work for a long decimal number, due to overflow!

You might as well, look into some concept of code indendation, that code which you have posted has a horriable indendation! And it also give a me a nasty taste of you using a very old Turbo C compile??? Start looking at some better compiler. DEV-C++, Code::block

ssharish

ssharish2005
Posting Whiz in Training
253 posts since Dec 2006
Reputation Points: 73
Solved Threads: 20
 

Thanks everyone, I finally got it right!

campuzcrazyness
Newbie Poster
17 posts since Jan 2008
Reputation Points: 6
Solved Threads: 0
 

Why should we worry about converting the number to hex or binary. Its already stored in binary format.

Prabakar
Posting Whiz
342 posts since May 2008
Reputation Points: 94
Solved Threads: 33
 

Well converting hex to binary is just problem. But there is one more reason. It is difficult to remember the binary pattern. So we group them to form it small number which still referees the same binary patten. There no any reason on why do we have to convert it binary to hex. At at least i havn;t come across any application which need a that feature!

ssharish

ssharish2005
Posting Whiz in Training
253 posts since Dec 2006
Reputation Points: 73
Solved Threads: 20
 
There no any reason on why do we have to convert it binary to hex. At at least i havn;t come across any application which need a that feature!


I have.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,492 posts since May 2006
Reputation Points: 3,348
Solved Threads: 943
 

char a[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
void dectohexa(int val)
{
if(val == 0) return;
dectohexa(val / 16);
printf("%c", a[val%16]);

}

r.ranjan
Newbie Poster
1 post since Oct 2010
Reputation Points: 10
Solved Threads: 0
 

Try dis :

Program to convert input decimal value to its hexadecimal equivalent

#include <stdio.h>
#include <conio.h>
#include <math.h>
void dtoh(int d);
void main()
{
 int d;
 clrscr();
 printf("Enter a no. in decimal system:-  ");
 scanf("%d",&d);
 dtoh(d);
 getch();
}

void dtoh(int d)
 {
  int b,c=0,a[5],i=0;
  b=d;
  while (b>15)
  {
   a[i]=b%16;
   b=b/16;
   i++;
   c++;
  }
  a[i]=b;
  printf("Its hexadecimal equivalent is  ");
  for (i=c;i>=0;--i)
  {
   if (a[i]==10)
	printf("A");
   else if (a[i]==11)
	printf("B");
   else if (a[i]==12)
	printf("C");
   else if (a[i]==13)
	printf("D");
   else if (a[i]==14)
	printf("E");
   else if (a[i]==15)
	printf("F");
   else
	printf("%d",a[i]);
  }
  return;
 }
msdinesh
Newbie Poster
1 post since Aug 2011
Reputation Points: 6
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You