Thread: convert word to binary

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    21

    convert word to binary

    How can I convert a word such as 'A' to the number represented in ASCII ? Another question is how can i convert a decimal number to binary number? Is any function in c allows us to do that?

    Thanx for any help

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    int num;
    num = (int) 'A';

    To convert a int to a binary string. One way is function itoa(), if your compiler has it.
    Code:
    #include <stdlib.h>
    int main(void)
    {
       int num = 255;
       char str[33];
       itoa(num,str,2);
    }
    And here's a clever function to print in binary that someone posted:
    Code:
    void printbits( int x )
    {
       int y;
       for( y = 31; y >= 0; y-- ) printf("%d",(x&(1<<y))?1:0);
          printf("\n");
    }

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    4
    will this also work?
    int i;
    i='A';
    printf("%d",i)//prints ASCII value of 'A'

  4. #4
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    yes

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >will this also work?
    Yes

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    21
    big thanx for you guys!!
    now another question is how can I mask those binary #
    for ex:

    111000101
    if i want shift right to 3, it will become
    101111000,
    just like what shr does in assembly language, but how can i do that in c?
    is any function allows us to do that?
    Last edited by Supra; 03-29-2002 at 05:57 PM.

  7. #7
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    X>>Y (Shifts X's bits Y steps to the right)
    X<<Y (Shifts X's bits Y steps to the left)
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  8. #8
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    asm
    {
    DW BINARY NUMBER = "=x"
    }

    As long as you can find a way of replacing x with the number, that works...

  9. #9
    Registered User
    Join Date
    Sep 2001
    Posts
    21
    really big thanks to you all!!
    my last two questions are

    1. how can i "and" or "or" those binary in c#?

    ex:
    Code:
                1100110
       (and)    0011100
    ----------------------
                0000100
    2. how can i convert binary # back to decimal #?
    or where can i find the tutorial web site about them?

    Thanx again

  10. #10
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    >1. how can i "and" or "or" those binary in c#?

    Well in C:

    & - binary AND
    | - binary OR

    I don't know about C#, but I guess it's the same as in C.

    >2. how can i convert binary # back to decimal #?
    >or where can i find the tutorial web site about them?

    Binary number

    bn ... b3 b2 b1 b0

    Decimal

    2^0 b0 + 2^1 b1 + 2^2 b2 + ... + 2^n bn

  11. #11
    Registered User
    Join Date
    Oct 2001
    Posts
    197
    Not again!!!

    Hereīs my code; itīs my first real C-program:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define PUFFER 256
    
    
    void asctobin(void); /* converts ASCII-characters into binary digits */
    void bintoasc(void); /* converts binary digits into ASCII characters */
    
    unsigned char bin[]={1,2,4,8,16,32,64,128};
    
    int main(void)
    {
    	char chose;
    
    	for(chose=0;chose<40;chose++,printf("\n")) ; /* clear screen */
    
    again:
    	printf("Enter \'A\' to convert ASCII to binary or \'B\' to convert binary into ASCII: ");
    	scanf("%c",&chose);
    	if(chose=='A' || chose=='a') asctobin();
    	else
    	{
    		if(chose=='B' || chose=='b') bintoasc();
    		else{
    			rewind(stdin); /* back to the begin of stdin; if more then one character was entered */
    			goto again;
    		} /* only accepts 'A','a','B' and 'b' as inputs */
    	}
    
    	return 0;
    }
    
    
    void asctobin(void)
    {
            char string[MAX],c;
    	int count,len;
    
    	printf("Enter string (max length: 255 characters): ");
    	rewind(stdin); /* back to the begin of stdin */
    	fgets(string,PUFFER-1,stdin);
    
    	len=strlen(string);
    
    	for(;len>0;len--) /* as often as the value for the length of the inputed string */
    	{
    		c=*string; /* the character string points to at the moment */
    		for(count=7;count>=0;count--)
    		{        
    			if(c>=bin[count])
    			{
    				printf("1");
    				c-=bin[count];
    			}else
    			printf("0");
    		}
    		*string++;
    		printf(" ");
    	}
    }
    
    
    void bintoasc(void)
    {
    	char string[MAX],c;
    	int count=7,len;
    
    
    	printf("Enter digits (max 255):");
    	rewind(stdin); /* back to the begin of stdin */
    	fgets(string,PUFFER-1,stdin);
    
    	len=strlen(string);
    
    	for(;len>0;len--) /* as often as the value for the length of the inputed string */
    	{
    		if(*string=='1')
    		{
    			c+=bin[count], --count;
    		}else 
    		if(*string=='0') --count;
    
    		if(count==0)
    		{
    			printf("%c",c);
    			count=7;
    			c=0;
    		}
    
    		*string++;
    	}
    }
    klausi
    Last edited by klausi; 04-01-2002 at 10:50 AM.
    When I close my eyes nobody can see me...

  12. #12
    Registered User
    Join Date
    Sep 2001
    Posts
    21
    thanx you all very much, but i still have questions about masking
    number
    I wrote a small program:
    Code:
    #include <stdlib.h>
    int main(void)
    {
       unsigned char i='a';
       unsigned char b='c';
       char str[33];
       i='a'+'c';
       printf("%d\n",i);
       itoa(i,str,2);
       printf("%s\n", str);
       i << 3;
       printf("%d\n",i);  //still can't shift
       i & 3;
       printf("%d\n",i); // still can't and
       return 0;
    }
    I can't mask the number
    cna anyone tell me what's wrong with it?
    really appreciate

  13. #13
    Unregistered
    Guest
    Originally posted by Supra
    i << 3;
    i & 3;
    I can't mask the number
    cna anyone tell me what's wrong with it?
    really appreciate
    That is the same as typing this somewhere in your code:

    1+6;

    As you see, it does nothing. You have to store your result somewhere:

    i=(i << 3); and i=(i & 3);

    Or in an even simplier way:

    i <<= 3; and i &= 3;

    Good luck!

  14. #14
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    That was me replying above, for some reason you're not logged in if you enter this site from Hotmail.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. convert string of binary to Char
    By yagmai in forum C Programming
    Replies: 2
    Last Post: 03-10-2008, 12:37 PM
  2. Weird thing convert float to binary
    By sara.stanley in forum C Programming
    Replies: 9
    Last Post: 02-13-2006, 09:17 AM
  3. Wrong Output
    By egomaster69 in forum C Programming
    Replies: 7
    Last Post: 01-28-2005, 06:44 PM
  4. Using 'if' with char arrays or string objects
    By c++_n00b in forum C++ Programming
    Replies: 36
    Last Post: 06-06-2002, 09:04 PM
  5. Convert a text file to a binary file
    By Cyber Kitten in forum C Programming
    Replies: 16
    Last Post: 02-04-2002, 08:53 AM