Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Read 0 and 1 text file as binary in Qt

I want to read a text file that contains 0 and 1 as binary, consider this example:

textfile.txt:

001001100101

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

010100011100

100000110001

001111110101

100010110101

111010100100

011011000110

i want to read this stream of 0 and 1 as binary or read then convert it. If we consider first line "001001100101", i want to it convert to 613 decimal and then with bit field or something like that, i check every bit of every number that was read. Only way that can do is read line by line and then parse to string after that check every splitted string with "1" or "0" and find real value. I think there is a simple way for this problem.

Thanks a lot.

>Solution :

If there are only 0 and 1 in your file,

you can use stl std::stoi(const string& str, [size_t* idx], [int base])

    string bin_string = "001001100101";
    int number = stoi(bin_string, nullptr, 2);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading