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..

Dani AI

Generated

Quick corrections and practical points for converting decimal to hexadecimal in C.

Hex digits run 0–9 then A–F (values 10–15). There is no "G" — decimal 16 is hexadecimal 10, not a single letter. As noted, mapping stops at F. The simplest, safest output is to use the library formatter (printf("%X", val) or printf("%x", val)), which and suggested; prefer snprintf/printf over sprintf to avoid buffer overruns.

A manual algorithm works like this: if the value is zero print 0. Otherwise repeatedly compute remainder = value % 16, save that remainder, then divide value /= 16. When finished, print the saved remainders in reverse order, mapping remainders 10..15 to A..F. Two common ways to produce the correct order are (1) push remainders into a small array and print the array backwards (the array approach mentioned by ), or (2) print recursively so higher-order digits appear first (the recursion shown by ).

Practical tips: pick buffer size by type — unsigned 32-bit needs up to 8 hex digits, unsigned 64-bit up to 16. Cast to an unsigned type when printing hex to avoid sign-extension of negative values. Avoid nonstandard getche(); use getchar() or line input (fgets + strtol) for robust parsing as recommended. Finally, remember 's point: converting to hex is only a change of representation for the same binary value, not a change to the underlying number.

Recommended Answers

All 15 Replies

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;
}

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

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.

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

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.

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 function

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

Thanks everyone, I finally got it right!

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

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

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.

commented: same here ... +4
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]);


}

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;
 }
commented: 3 years late, bad non-standard code, bad formatting, no CODE tags. Good job! -4
//Decimal to Hex value 
    main()
    {
       char hex[16]={"0123456789ABCDEF"};
       int num,i=0,j;
       char result[];
       printf("Enter the Decimal No.\n");
       scanf("%d",&num);
       while(num)
       {
         result[i]=hex[num%16];
         num=num=num/16;
         i++;
       }
       printf("Given num of the hex value is:");
        for(j=i-1;j>=0;j--)
        printf("%c",result[j]);
        getch();
        return 0;
    }
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.