DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: Search bit string in file

  1. #1
    Join Date
    Feb 2007
    Posts
    3

    Search bit string in file

    Hello!

    I'm looking for an algorithm which finds all occurences of a bit sequence (e.g., "0001") in a file. This sequence can start at any bit in the file (it is not byte aligned).

    I have some ideas of how to approach the problem (1) reading file into unsigned char buffer, 2) defining bit structure, 3) comparing the first 4 bits of the buffer with the bit structure, 4) shifting the char buffer one left, 5) repeat at step 3)) but I'm not sure if this is a good or the right approach?

    It would be great to get some input, possibly with sample source code.

    Many thanks,
    Michael

  2. #2
    Join Date
    Jan 2005
    Location
    UK
    Posts
    604
    Hi,
    why not using the std::find() algorithm to find the first binary digit in your string
    and then "munch up" all digits until you find a non-digit.
    something like:
    Code:
    string myString = "lgdha;0110101001dajhglg10101asyhdf001001";
    size_t startPos = find_first_of("01");
    size_t endPos = find_first_not_of("01",startPos);
    string result = myString.substr(startPos,endPos-startPos-1); // should be the first
                                                                                      // substring that represents a binary
    Mind you I haven't tested this, but a little investigation will get you started in no time ;-)

    Cheers,

    D
    DKyb
    -------------------------------
    Life is a short warm moment -
    Death is the long cold rest.
    Pink Floyd
    -------------------------------

  3. #3
    Join Date
    Feb 2007
    Posts
    3
    Hi!

    Sorry, I think I did not make myself clear. I'm talking about bit strings / bit sequences here, as in: an integer is a 8 bit type. I'm talking about the binary level, not about finding substrings in strings.

    Thanks,
    Michael

  4. #4
    Join Date
    Jan 2005
    Location
    UK
    Posts
    604
    Sorry misunderstood you there.
    But you still can use the STL.
    For instance you could translate the bytes of your file into a bit-string (the string will bit 8 times the size of the file. If that is a problem, read your file in chunks).
    and then do a find on the string. that shoud do the trick.
    Cheers,
    D
    DKyb
    -------------------------------
    Life is a short warm moment -
    Death is the long cold rest.
    Pink Floyd
    -------------------------------

  5. #5
    Join Date
    Feb 2007
    Posts
    3
    Sounds interesting, can you give a code example?

    Can I also modify the binary file that way, i.e., translate it into a string, manipulate the string, and translate it back into binary?

    Thanks,
    Michael

  6. #6
    Join Date
    Jan 2005
    Location
    UK
    Posts
    604
    Of course you can.
    Code:
    string charToBitString(unsigned char theByte)
    {
         string reval("00000000");
         unsigned char testBit = 128;
         for(int i=0;i<8;i++)
         {
              if((testBit & theByte) == testBit)
                  reval[i] = '1';
         }
         return reval; // here reval should hold the bit-pattern
    }
    
    ...
    string fileAsABitString("");
    // call in a loop for every byte of the file
         fileAsABitString += charToBitString(fileByte);
    
    ...
    Again, all the spellos read in the right order form an old serbo-kroatian saying, so I leave
    the debugging to you...

    Cheers,

    D
    DKyb
    -------------------------------
    Life is a short warm moment -
    Death is the long cold rest.
    Pink Floyd
    -------------------------------

Similar Threads

  1. Packed Data(Comp-3, etc)
    By Marcos in forum VB Classic
    Replies: 3
    Last Post: 01-25-2006, 12:18 PM
  2. How long before the next version??
    By _CAG in forum .NET
    Replies: 146
    Last Post: 08-12-2002, 10:40 PM
  3. App Object
    By Rob Teixeira in forum .NET
    Replies: 15
    Last Post: 05-31-2002, 03:30 PM
  4. NullPointerException when reading text file
    By Andrew McLellan in forum Java
    Replies: 3
    Last Post: 05-09-2001, 05:34 PM
  5. Replies: 0
    Last Post: 06-22-2000, 07:30 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center