Break up a string

This is a discussion on Break up a string within the C Programming forums, part of the General Programming Boards category; OK guys here is my problem. I have an string that consist of 8 bits all 1s and 0s. These ...

  1. #1
    Registered User cdonlan's Avatar
    Join Date
    Sep 2004
    Posts
    49

    Break up a string

    OK guys here is my problem. I have an string that consist of 8 bits all 1s and 0s. These strings are scanned in from a text file. I have to take these strings and do severl logic test with them(add,or,nagation,and...) I think I need to save the string as a single characture and do the test with it. What function can i use to scan in a single char off a string and at it to a char that will contain one byte when it is dont.

    I believe it is going to involve a for statement, but I dont know what will scan in a single char.

    Thanks
    Chris

  2. #2
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    I think I need to save the string as a single characture and do the test with it
    And... what makes you say that? Are you possibly confusing this with pointers, which will look like single chars to the "untrained-observer". Or if you're referring to just having a for loop that looks at one character at the same time, is there a reason that you can't just use

    Code:
    char c
    for(int i = 0; i < string_length; i++)
    {
         c = string[i];
         // Tests performed on c
    }

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    I think he means something more like this:
    Code:
    int main(void)
    {
      char str[] = "10011011";
      unsigned char ch = 0;
      int i;
    
      for(i = 0;str[i];i++)
        if(str[i] == '1')
          ch += 1 << (7-i);
    
      return 0;
    }
    And then he can use ch for all of his bit operations.
    If you understand what you're doing, you're not learning anything.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    >What function can i use to scan in a single char off a string and at it to a char that will contain one byte when it is dont.

    Why not go 8 "bits" at a time and use strtoul with base 2?
    Code:
     char str[] = "10011011";
      unsigned char ch = strtoul(str, NULL, 2);
    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.*

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    Well sure...if you want to do it the easy way
    If you understand what you're doing, you're not learning anything.

  6. #6
    Registered User
    Join Date
    Nov 2004
    Location
    bombay
    Posts
    6
    in order to scan a single character use getchar() and putchar()
    functions in order to split a string use the pointers to the string for more help email me @ gayomard@gmail.com


    always happy to help

  7. #7
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317
    What about a union?

    Code:
    union Bitstring
    {
        char string[8];
        struct 
        {
            char b1;
            char b2;
            char b3;
            char b4;
            char b5;
            char b6;
            char b7;
            char b8;
        }charbit;
        
    };
    That will let you treat it as individual bits or as a string. I think I read about this in another post?
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why? You can do the same thing with an array:
    Code:
    char bitsbutnotreally[ size ] = { "10110010" };
    
    bitsbutnotreally[ bitnumber ] = '0';
    I suppose a union would work, but I don't see the point in the added complexity. Except of course just because you can.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. ascii rpg help
    By aaron11193 in forum C Programming
    Replies: 18
    Last Post: 10-29-2006, 01:45 AM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  4. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  5. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 10:06 PM

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