Thread: how to print bits?

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    30

    how to print bits?

    hi

    I'm trying to print in binary the data stored in a string..
    here's the string:

    char test[20];
    strcpy(test, "any data");

    so how to print the bits?
    is there an escape sequence that i can use with printf to do that?


    thanks

  2. #2
    Registered User
    Join Date
    Nov 2004
    Location
    USA
    Posts
    516
    take in each character,convert it into binary form, and output to the screen..
    Code:
    void bin(int a)
    {
     if(a/2==1)
     printf("1");
     else
     bin(a/2);
     printf("%d",a%2);
    }
    this function converts an integer into binary form and displays it...i leave it to you to modify this to print the string...it wont be difficult.
    Code:
    >+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    30
    hum are you sure about this function? this will work for numbers between 0 and 10...I guess I'll have to add a loop..

  4. #4
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    it looks like it works for numbers > 10...

  5. #5
    Registered User
    Join Date
    Nov 2004
    Location
    USA
    Posts
    516
    hum are you sure about this function? this will work for numbers between 0 and 10...I guess I'll have to add a loop..
    you will have to add a loop and yes, i am pretty much sure bout this function.
    Code:
    >+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by majoub
    I'm trying to print in binary the data stored in a string.
    You could do something with this bits_block.
    Code:
    int main(void)
    {
       const char test[] = "any data";
       char binary [ (CHAR_BIT + 1) * sizeof test ];
       puts(test);
       puts(bits_block(binary, test, sizeof test));
       return 0;
    }
    
    /* my output
    any data
    01100001-01101110-01111001-00100000-01100100-01100001-01110100-01100001-00000000
    */
    Last edited by Dave_Sinkula; 03-19-2005 at 12:34 PM. Reason: Added code.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Integer and the no. of bits it occupy
    By ramayana in forum C Programming
    Replies: 2
    Last Post: 12-15-2005, 10:06 AM
  2. Replies: 1
    Last Post: 07-31-2002, 11:35 AM
  3. Help Please...bits
    By Unregistered in forum C Programming
    Replies: 11
    Last Post: 01-24-2002, 01:43 PM
  4. Questions on Speed of Execution
    By wavering in forum C Programming
    Replies: 22
    Last Post: 01-20-2002, 02:04 PM
  5. How to print out the data throw printer.Attn Salem
    By Jason in forum C Programming
    Replies: 2
    Last Post: 09-23-2001, 05:58 AM