Sorry, but I'm working for a industry and they choose to use binary files (I imagine for security reasons, since I read bin files are more difficult to fall under reverse engeneering), so there is nothing I can do about it; I must learn how to convert text to bin and how to read bin and transform it into ASCII.
Security reasons? Rubbish, unless the binary files are encrypted and that is unlikely. The data stream is most likely binary, i.e. not human readable, because that is a convenient, compact form for the source to emit. You need to know what the bytes in the file represent, how to group them into larger units (ints, floats, etc.), and into larger structures. Being able to dump the raw byte stream in a human readable form may help understand what is in the file, but it unlikely to be the end goal.
For example, a (fictitious) data acquisition unit might gather samples from n inputs and output a variable length data structure like this for each sample set:
struct sample {
ulong byteOrder, // 32-bits value 0xFFFE0000
char senderName[16], // ASCII chars only, space padding, no termination NUL
ulong sequence, // 32-bits
ushort n, // 16 bits
double samples[n], // 64-bits per sample, IEE754 format
ulong crc // CRC-32, excludes byteOrder and crc fields
};
struct sample {
ulong byteOrder, // 32-bits value 0xFFFE0000
char senderName[16], // ASCII chars only, space padding, no termination NUL
ulong sequence, // 32-bits
ushort n, // 16 bits
double samples[n], // 64-bits per sample, IEE754 format
ulong crc // CRC-32, excludes byteOrder and crc fields
};
To copy to clipboard, switch view to plain text mode
Your problem is how to interpret that at the receiving end.
Thanks, Chris, for the functions. I only didn't understand the explanation regarding QDataStream. What do you mean by "aimed at providing an object serialisation system for Qt applications"?
I mean it is a way to write binary data stream in one Qt program that can be read by another Qt program using any version of Qt (within limits set by the writer), with any native machine endianness, and reconstruct the objects (Qt or user) at the other end. That applies to simple types like int or char as well as to complex structures like QString, QList<QPair<QString,QPixmap> > etc.
You can use it on arbitrary data streams but it may be easier not to depending on the structures involved.
are you sure these are the functions and your explanation is correct?
Yes
I was able to use QString::toLong to convert from byte to ASCII
Are you sure? Read the documentation for QString::toLong()... Hint: it doesn't return a QString.
, but my efforts to use QString::number are being useless.
Probably because you seem very confused.
Read the documentation for QString::number()... Hint, it doesn't convert strings into numbers.
bool ok(false);
QString strBinary
("00000010");
// That's 2 in 8 binary bits. // This is a human-readable STRING representation of a number. This is not what a "binary" file contains.
long result = strBinary.toLong(&ok, 2);
qDebug() << result << ok;
// Output: 2 true
// Result is the number represented by the binary digits above, and the conversion was successful.
// The output is a human-readable decimal representation of that number.
qDebug
() <<
QString::number(result,
2);
// Output: "10"
// This is a human-readable STRING representing the number (minus leading zeros)
bool ok(false);
QString strBinary("00000010"); // That's 2 in 8 binary bits.
// This is a human-readable STRING representation of a number. This is not what a "binary" file contains.
long result = strBinary.toLong(&ok, 2);
qDebug() << result << ok;
// Output: 2 true
// Result is the number represented by the binary digits above, and the conversion was successful.
// The output is a human-readable decimal representation of that number.
qDebug() << QString::number(result, 2);
// Output: "10"
// This is a human-readable STRING representing the number (minus leading zeros)
To copy to clipboard, switch view to plain text mode
Bookmarks