Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to partition a QByteArray message efficiently, insert gap after 1 byte.
QByteArray contain 8byte data.
EX:
QByteArray qba = AABBCCDDEEFF9988

I want to write this into txt file in the following format.
AA BB CC DD EE FF 99 88


Any help would be great =)
Posted
Comments
Andreas Gieriet 17-Dec-15 7:36am    
What is the problem? You do not know how to convert a byte value into a hex text representation? or how to add a space?
By what means "efficient"? Easy to write/read as a programmer? Quick execution? Little memory usage? ...?
Where does the text go to? Into an std::string or into a std::ostream, or anything else?
Cheers
Andi
XamBEE 17-Dec-15 7:43am    
how to add space ?
yes i want to add spaces

To make is easy to read as programmer
Andreas Gieriet 17-Dec-15 7:47am    
Pseudo code:

f = open file
foreach byte in bytearray do
t = convert byte to hex
write t to f
write space to f
end foreach
close f

What of the above mentioned pseudo code causes troubles?
Cheers
Andi

This can be best done using printf formatting:
FILE *f = fopen("test.txt", "w");
for (int i = 0; i < qba.size(); i++)
{
    if (i)
        fprintf(f, " ");
    fprintf(f, "%02X", (unsigned char)qba.at(i));
}
fprintf(f, "\n");
fclose(f);

If your file is already opened by another method than fopen, check if you can get a FILE pointer for the opened file or use sprintf with a local buffer and write that to the file.

[UPDATE]
For an existing QTextStream like from the comment it can be:
mylog << " " << currtTimeStump "," << MsgDLC << "," << frameID << ",";
for (int i = 0; i < qba.size(); i++)
{
    char dataByte[4];
    sprintf(dataByte, " %02X", (unsigned char)qba.at(i));
    mylog << dataByte; 
}
mylog << '\n';
 
Share this answer
 
v4
Comments
XamBEE 17-Dec-15 7:53am    
I am not reading it from the file. Actually i want to write into the file with spaces. so before writing into the file QByteArray qba = AABBCCDDEEFF9988.
here is one line of my txt file.
13:33:44:095, 10826196, 1, 0x701, 7faabbccddeeff66
I want to write something like
13:33:44:095, 10826196, 1, 0x701, 7f aa bb cc dd ee ff 66
Jochen Arndt 17-Dec-15 7:59am    
That is what my solution does:
- It opens the file test.txt for writing
- It loops through the number of bytes
-- It prints a space to the file if it is not the first byte
-- It prints a byte as a sequence of two hex characters
- It prints a new line character
- It closes the file
XamBEE 17-Dec-15 9:19am    
I get your point but i am not writing a file the method you mentioned.
here is my sample code......
void writefile(QString Filename)
{
QFile mFile(Filename);
if(!mFile.open(QFile::WriteOnly | QFile::Text))
{ qDebug() << "Not open file";
return;}
QTextStream mylog(&mFile);
mylog <<" "<< currtTimeStump ","<<"MsgDLC << ","<< frameID << ","<< Data << '\n';
mFile.close();

}
//Data contain QBYTEArray. which i want to make easily readable
Jochen Arndt 17-Dec-15 9:29am    
Therefore I have written the last sentence in the solution because you did not mentioned in your question what method you are using.

I will update my solution.
XamBEE 17-Dec-15 9:52am    
when QByteArray qba = 8000000000000000
output in .txt file is as following.
FFFFFF80 00 00 00 00 00 00 00 //first byte with three extra bytes.
To write a char (i.e. a "byte") as upper case hex string into an output stream, you might do the following:
C++
#include <iostream>
#include <iomanip>
...
ostream& toHex(ostream& output, unsigned char byte) {
    return output << hex
                  << uppercase
                  << setw(2)
                  << setfill('0')
                  << int(byte);
}
Usage:
C++
for(int i = 0; i < n; i++) {
    cout << toHex(cout, bytes[i]) << " ";
}
cout << endl;
Cheers
Andi
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900