how to print bits?

This is a discussion on how to print bits? within the C Programming forums, part of the General Programming Boards category; hi I'm trying to print in binary the data stored in a string.. here's the string: char test[20]; strcpy(test, "any ...

  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
    India
    Posts
    515
    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
    India
    Posts
    515
    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,006
    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 11:34 AM. 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, 09: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, 12:43 PM
  4. Questions on Speed of Execution
    By wavering in forum C Programming
    Replies: 22
    Last Post: 01-20-2002, 01: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21