• Status: Solved
• Priority: Medium
• Security: Public
• Views: 329

# ASCII string to binary conversion

Is there anyone here can help me writing a short C script to prompt the user for a string of 1-5 characters. Each character of the string should be output to an 8 bit binary number?
0
DacTin
1 Solution

Commented:
Hi DacTin,
sounds like homework.

Try doing the work, and when you get stuck, post a specific question.

David Maisonave :-)
Cheers!
0

Commented:
Keep in mind that a char in C *is* an 8-bit binary number.
0

Commented:
I think he means output 'A' as 01000001
0

Software ArchitectCommented:
You will need some boolean logic:
To know if first bit of a character is on, you can use the and (&) operator:

c = 'A';
if (c&1) {   /* is 1st bit on? */
/* some action here */
}
0

Commented:
You only have 26 letters. why don't just build an array to store binary pattern for each character? This takes little space (26*9 bytes) tand it is faster than testing each bit.
0

Commented:
And, a char in C is *not* a 8-bit binary number.

The Standard says a char in C *is* a byte. But the size of a byte varies on hardware.
0

Commented:
Please have a look at this

ASCII of 'A' = 0100 0001
ASCII of 'B' = 0100 0010
ASCII of 'C' = 0100 0011

Do you mean this ?

Then I have solution

Thanks..........
0

Commented:
If you think a person solves your question, you should accept his answer, assign points and close this question.
0

Author Commented:
I myself found the solution. Thanks for your suggestion.