Parsing array elements

This is a discussion on Parsing array elements within the C++ Programming forums, part of the General Programming Boards category; Okay here goes, Is it possible to parse the elements of an array (11110000) into separate boolean characters.... I have ...

  1. #1
    DMaxJ
    Guest

    Unhappy Parsing array elements

    Okay here goes,

    Is it possible to parse the elements of an array (11110000)
    into separate boolean characters.... I have an array of characters named 'buffer'

    char buffer[9]; // 8 chars plus the NULL
    gets (buffer) ; // I type in 11110000


    I want to parse each character to make an array of boolean characters.... How could I do this...

    Thanks

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Code:
    #include<stdio.h>
    
    
    
    void printbits(unsigned int tobeprinted)
    {
    unsigned int shift=8*sizeof(unsigned int)-1; // bits are 0-31 not 1-32
    unsigned int mask=1<<shift;
    printf("%i is ",tobeprinted);
    for(unsigned int i=1;i<=(shift+1);i++)
    {
    
    if(tobeprinted &mask) printf("1");
    else
    printf("0");
    tobeprinted <<= 1;
    if (i%8==0) printf(" ");
    }
    }
    
    int main()
    {
    for (int i=1;i<10;i++)
    {
    printf("Enter positive integer :-");
    unsigned int input;
    scanf("%i",&input);
    printbits(input);
    printf("\n");
    }
    return 0;
    }
    not exactly what you are after but it shows one possible method of extracting bits from an int.From this you will be able to do the same for a char.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array Elements Overwritten
    By Zoetermeer in forum C Programming
    Replies: 13
    Last Post: 10-09-2008, 06:10 PM
  2. Adding More Array Elements?
    By Vermillion in forum C++ Programming
    Replies: 2
    Last Post: 09-14-2008, 10:02 PM
  3. coping elements not in array
    By lord in forum C++ Programming
    Replies: 2
    Last Post: 08-04-2008, 07:53 PM
  4. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  5. A simple question about selecting elements in an array
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 08-30-2001, 10:37 PM

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