Find the binary form

Write a program to find the binary form for the given charcter input.
eg:- for 'A' ASCII value is 65 and its binary form is 1000001.

Questions by abhijeetsingh   answers by abhijeetsingh

Showing Answers 1 - 30 of 30 Answers

#include<stdio.h>
#include<conio.h>
#define MAX_SIZE 10

void main()
{
    int d,i,j[MAX_SIZE],k = MAX_SIZE - 1,l ;
    char a;

    clrscr();

    for ( i = 0 ; i <= MAX_SIZE ; i++ )
    {
        j[i] = 0 ;
    }

    printf("n Enter the Letter " ) ;
    scanf ( "%c",&a) ;

    printf ( "n The Input is            : %c",a) ;

    d = (int) a;
    printf ( " n The Decimal Format of %c is    : %d " ,a,d ) ;
    i = d ;


    do
    {
           l = i%2 ;
           j[k] = l ;
           i = i/2;
           k-- ;
    }while ( i != 0 );

    i = 0 ;
    l = 0 ;
    while ( j[i] == 0 )
    {
        i++ ;
        l++ ;
    }
    printf ( " n The Binary Format of %c is      : ",a );
    for ( i = l ; i < MAX_SIZE ; i++ )
    {
        printf ("%d",j[i]);
    }

    getch();

}

  Was this answer useful?  Yes

// Here is a simple recursive solution

#include <iostream>

void binaryForm(int dividend)

{

int quotient;if(dividend== 1)

{

printf("%d",dividend);

return;

}

else

{

quotient = dividend%2;

dividend = dividend/2;

binaryForm(dividend);

printf("%d",quotient);

}

}

int main()

{

char ch;int num;

printf("Enter a character :");

scanf("%c",&ch);

binaryForm(ch);

system("PAUSE");

return 0;

}

  Was this answer useful?  Yes

This is one of the way to find the binary value of the given number.

 #include
 #include
 main()
  {
   int  no,k,p=1;
   long int  bin=0;
   clrscr();
   puts("Enter any number to find the binaryn");
   scanf("%d",&no);
   dno=no;
 
   while(n)
                 {
                  k= no % 2;
                  bin = bin + p * k;
                  p = p * 10;
                  no = no / 2;
                  }
  puts("************************************n");
   printf("%dThe given number is   : "dno);
   printf("%l The binart format is  :",bin);
  puts("n***********************************"); 
}


 Thank U
 
  Ashok kumar
  asho . 28 AT gmail . com

  Was this answer useful?  Yes

//Another  Method

int main()
{
    int lBinaryValue = 0;
    char chInputChar;   
    printf("Enter a character");
    scanf("%c",&chInputChar);
    lBinaryValue =  ConvertDecimalToBinary((int)chInputChar);
    printf("%d", lBinaryValue);
    return 0;

}
int ConvertDecimalToBinary(int lInput)
{
    int lTempInt = lInput;
    int lBinaryOutput = 0;
    int i =0;   
    while(lTempInt > 0)
    {       
        lBinaryOutput = lBinaryOutput +(lTempInt%2)*((int)pow(10, i++));       
        lTempInt = lTempInt/2;
    }
    return lBinaryOutput;   
}

  Was this answer useful?  Yes

aman15490

  • May 20th, 2008
 

  /*a very simple solution*/
#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
int i,k,d=1,sum=0;
printf("enter the chracter");
scanf("%c",&ch);
i=ch;
while(i>=1)
{
  k=i%2;
  sum=sum+k*d;
  i=i/2;
  d=d*10;
 }
printf(" binary is %d"=sum);
getch();
}

  Was this answer useful?  Yes

jintojos

  • May 22nd, 2008
 

  #include<conio.h>
  #include<stdio.h>
  void fun(int);
  void main()
    {
       char ch;
       clrscr();
       printf("Enter The Char :");
       scanf("%c",&ch);
       printf("The Binary Of %d Is :");
       fun(ch);
       getch();
    }

  void fun(int num)
    {
       int rem;
       if(num==0) return;
       rem=num%2;
       fun(num/2);
       printf("%d",rem);
    }

  Was this answer useful?  Yes

kernel32

  • May 22nd, 2008
 

#include<stdio.h>

void recDec2Bin(int dec)
{
  if(dec == 1){
    printf("1");
  }else{
    //printf("%d",dec%2); // to print it inverse
    recDec2Bin(dec/2);
    printf("%d",dec%2);
  }
}

int main(int argc, char *argv[])
{
  if(argc > 2){
    printf("Error: Number of argument");
  }else{
    recDec2Bin(atoi(argv[1]));
  }
  printf("n");
  return 0;
}

  Was this answer useful?  Yes

kotsiman

  • Jun 21st, 2008
 

main()
{
  unsigned char a;
  unsigned char mask = 0x80;
  clrscr();
  puts("Enter any single charactern");
  scanf("%c",&a);
  puts("binary equvalent of your nuber is ");
  while(mask)
  {
      (a & mask)? printf("1"):printf("0");
      mask >>= 1;
  }
  getch();
}


Here is a simple code which will just get each bit of the char by AND operation


int main ()
{
    char a; int i,j;
    printf("Enter a charn");
    scanf("%c",&a);
    printf("The binary value of %c =",a);
    for (i=0,j=128; i<8; i++,j>>=1)
    {       
     if ((a & j))
     {
           printf("1");
     } else {
           printf("0");
     }    
    }
    fflush(stdin);
    getchar();
}

  Was this answer useful?  Yes

kbjarnason

  • Jul 1st, 2010
 

This seems a rather pointless endeavour, as the binary representation of, say, 'a' will vary with the execution character set.    For example, in EBCDIC, 'a' has a value of 128, whereas in ASCII, it has a value of 97.

If you need some form of vaguely sensible binary representation, you'll have to do something along these lines:

char alphanum[] = "ABCD...abcd...0123...etcetcetc";

Now look up the character (eg 'a') in the alphanum array, where it will have a constant value regardless of execution character set[1], then convert that value to binary.


[1] Assuming the character is represented in the execution character set at all; some aren't.  For examples of this, take a look at trigraphs.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions