简体   繁体   中英

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM