1

I need help writing a program that converts full sentences to binary code (ascii -> decimal ->binary), and vice-versa, but I am having trouble doing it. Right now I am working on ascii->binary.

ascii characters have decimal values. a = 97, b = 98, etc. I want to get the decimal value of an ascii character and convert it to a dinary or binary decimal, like 10 (in decimal) in binary is simply:

10 (decimal) == 1010 (binary)

So the ascii decimal value of a and b is:

97, 98

This in binary is (plus the space character which is 32, thanks):

11000011000001100010 == "a b"

11000011100010 == "ab"

I have written this:

int c_to_b(char c)
{
    return (printf("%d", (c ^= 64 ^= 32 ^= 16 ^= 8 ^= 4 ^= 2 ^= 1 ^= 0));
}

int s_to_b(char *s)
{
    long bin_buf = 0;

    for (int i = 0; s[i] != '\0'; i++)
    {
        bin_buf += s[i] ^= 64 ^= 32 ^= 16 ^= 8 ^= 4 ^= 2 ^= 1 ^= 0;
    }

    return printf("%d", bin_buf);
}

code examples

main.c

int main(void)
{
    // this should print out each binary value for each character in this string
    // eg: h = 104, e = 101
    // print decimal to binary 104 and 101 which would be equivalent to:
    // 11010001100101
    // s_to_b returns printf so it should print automatically
    s_to_b("hello, world!");
    return 0;
}

To elaborate, the for loop in the second snippet loops through each character in the character array until it hits the null terminator. Each time it counts a character, it gets does that operation. Am I using the right operation?

11
  • What does "converts full sentences to binary code" mean? Commented Dec 27, 2011 at 6:05
  • 7
    You may need to narrow this down to something more specific. Generally "doesn't work, do it for me" isn't very productive. Commented Dec 27, 2011 at 6:06
  • Why do you code c ^= 64 which means c = c ^ 64 ? And I don't understand if it is homework, and what is your requirement?? Commented Dec 27, 2011 at 6:10
  • 2
    You might need to re-thing your understanding of character sets. Everything in a computer is represented in binary, thus ASCII is a "binary code" in the sense that every ASCII character is a number represented in binary. Perhaps you should give an example of the desired transformation as well as a detailed descriptions of how it should work. Commented Dec 27, 2011 at 6:12
  • 2
    Your description is still unclear. It seems there are hidden rules, such as spaces. A space is character 32 in ASCII, which would give it a binay code of 00100000, making "a b" map to "1100001001000001100010", not "1100001 1100010". Commented Dec 27, 2011 at 6:27

1 Answer 1

2

Maybe you want something like

void s_to_b(const char*s)
{
  if (s != NULL) {
     while (*s) {
        int c = *s;
        printf(" %d", c);
        s++;
     }
     putc('\n');
  }
}
Sign up to request clarification or add additional context in comments.

4 Comments

yes exactly like this, but i believe it prints out the characters in decimal (it printed values over 1, like 9). is there a way to make the characters print out in binary rather than decimal?
Code a function print_in_binary and replace the call to printf with print_in_binary(c);
here is what i came up with: int print_in_binary(char c) { int q = c; while ((q / 2) != 0) { q = (c / 2); } printf(" %d", q); }
This probably prints the bits but from last to first. You should put them in an array first, then display in reverse order.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.