1

I am trying to write a binary document. So far, I succeeded in writing a .bin where the content has an hexadecimal format. I would like to have the content in a binary format (0 and 1 only).

Is it possible ? How to do it ? Am I wrong in my idea and having it in hexadecimal or binary is the same thing ?

Here is my code so far :)

#include <QApplication>
#include <QDataStream>
#include <QString>
#include <QFile>
#include <iostream>
#include <QDebug>
#include <QTextStream>
void createBinaryFile()
{
    int a = 22;

    QFile file("/home/.../facts.bin");
    if (!file.open(QIODevice::WriteOnly)) {
        std::cerr << "Cannot open file for writing: "
              << qPrintable(file.errorString()) << std::endl;
    return;
    }
    QDataStream out(&file);
    out.setVersion(QDataStream::Qt_4_3);
    out << quint32(0x12345678) << a;
    QTextStream writeInConsole(stdout);
    writeInConsole << a;

    file.flush();
    file.close();

}
void readBinaryFile()
{
    quint32 n;
    int a;
    QFile file("/home/.../facts.bin");
    if (!file.open(QIODevice::ReadOnly)) {
        std::cerr << "Cannot open file for reading: "
              << qPrintable(file.errorString()) << std::endl;
    return;
}

QDataStream in(&file);
in.setVersion(QDataStream::Qt_4_3);

in >> n >> a;
QTextStream okk(stdout);
okk << a;
}

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
createBinaryFile();
//readBinaryFile();

return app.exec();
}

And this is the content of facts.bin : 1234 5678 0000 0016

Thank you very much for your help ! :)

7
  • Every computer store internally only 0 and 1's. Hex is just an easy way to see its contents. Commented Mar 14, 2018 at 16:31
  • Yes, that is why I had a doubt about the need to convert them in my file. But the thing is I need to read a .bin that has contents in bits and not hexadecimal notation :P Commented Mar 14, 2018 at 16:39
  • That is what I said. They are only stored in binary. Hex is just a convenient way to see it. There is no conversion to be done, just the way you want to present it. This mean that when you see 0xF, internally they are stored as 1111 in binary (assuming only 4 bits) Commented Mar 14, 2018 at 16:42
  • You don't want to be using formatted operations (i.e. operator<<). By definition, they format numbers into readable values. And you don't want a QDataStream - as its documentation says, it's a platform-independent transfer representation. You probably want to writeRawData() or simply use your QIODevice directly (i.e. QIODevice::write()). Commented Mar 14, 2018 at 16:48
  • 2
    "So far, I succeeded in writing a .bin where the content has an hexadecimal format" as viewed by what? It's not clear what you are saying. You either have a binary file or you don't. The rest is merely a matter of how you are viewing the data, and you may be viewing it in a hex editor, for example. You also forgot to show us what the desired content is... only the wrong one... Commented Mar 14, 2018 at 16:56

1 Answer 1

2

This is a non issue. Binary, octal, decimal, hexadecimal and so on - that's just different representation of the same data.

Computers are binary, and all data stored on a computer is therefore binary data.

Don't be tricked by the default/preferable way binary data is output to humans, which is usually hexadecimal for the sake of brevity, and for technical and historic reasons.

The smallest thing computers can address is a byte. Unless you pack data explicitly, boolean values are stored as bytes too, even though they only require a single bit in theory. You cannot really read or write a single bit, that operation would involve reading or writing the container byte plus some bitwise operations to extract or splice in the desired bit.

And finally, keep in mind that data is very often actually not stored in a binary format but as "text" that needs additional parsing in order to be read. It is such cases where the numeric system is important, because this tells the computer how to interpret the text in order to convert it to the proper binary data. So that it knows whether a 101 is a 5 (bin), or a 65 (oct), or a 101 (dec) or a 257 (hex).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.