Hi,

I want to write a program to calculate the no. of bytes between the first binary pattern (110) to the next 110 binary pattern. i know i probably have to make use of:

cout<<sizeof(char);

Do i need to use a pointer or something to tell the program where to start counting? Or is there a better way?

Thanks.

Recommended Answers

All 3 Replies

Unless you need something special, it would be easier to convert the bits to a string and do string searches. Then the problem is trivial.

Hi Narue,
I have already got the code of searching 001 byte pattern ready. So i was thinking how do i use this info to calculate the no. of bytes between two 001 byte pattern.

#include<iostream>
#include<fstream>

using namespace std;

bool isH264(ifstream& stream)
{
	int numZeros = 0;
	while(stream.good())
	{
		char c = stream.get();
		if(c == 0)
			numZeros++;
		else if(c == 1 && numZeros == 2)
		{
			return true;
		}
		else
			numZeros = 0;
	}
	return false;
}

int main(int argc, char *argv[])
{
	ifstream file("MOV_0003.mp4", ios::binary);
	if(isH264(file))
		cout << "This may be a H.264 file" << endl;
	else
		cout << "This is not a H.264 file" << endl;
	file.close();
	cin.ignore();
cin.get();
	return 0;
}

Nevermind, I was thinking bit patterns instead of bytes, but you asked about bytes. This is much easier, just begin incrementing a counter after you find the first matching byte and stop when you find the second. For example:

#include <iostream>
#include <cctype>

int main()
{
    bool found_start = false;
    int range = 0;
    char c;

    while (std::cin.get(c)) {
        if (std::isspace(c)) {
            if (!found_start)
                found_start = true;
            else {
                std::cout<< range <<'\n';
                found_start = false;
                range = 0;
            }
        }
        else if (found_start) {
            ++range;
        }
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.