# Thread: convert word to binary

1. ## 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. 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. will this also work?
int i;
i='A';
printf("%d",i)//prints ASCII value of 'A'

4. yes

5. >will this also work?
Yes

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

7. X>>Y (Shifts X's bits Y steps to the right)
X<<Y (Shifts X's bits Y steps to the left)

8. asm
{
DW BINARY NUMBER = "=x"
}

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

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

12. 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;
}```
cna anyone tell me what's wrong with it?
really appreciate

13. Originally posted by Supra
i << 3;
i & 3;
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. That was me replying above, for some reason you're not logged in if you enter this site from Hotmail.