Home > Software engineering >  Read 0 and 1 text file as binary in Qt
Read 0 and 1 text file as binary in Qt

Time:12-23

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

textfile.txt:

001001100101

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.

CodePudding user response:

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);
  • Related