Hi,
I am new to QT, but have been programming in C/C++. I have a question regarding displaying hex values to TextEdit Widget. I have tried various method, but sometimes some of the hex values change or sometime the output is totally different.
So I have a char buffer like this:
1st implementation:
char * cHexData = new char [HexBuff_size+1];
memset(cHexData, '\0', HexBuff_size+1);
char * temp_iter = cHexData;
for (int i = 0; i<(HexBuff_size+1); i++){
sprintf(temp_iter ,"%X",(unsigned char)HexBuff[i]); //HexBuff is char buffer of size 256 and it holds the some data
temp_iter++; // I am printing putting the values to cHexData buffer which will hold hex values
} //ex: cHexData = FF6C69.....
//now i need to convert it to QString so that I can out the same Hex value in cHexData with formatting
for (int i = 0; i<(HexBuff_size+1); i++){
if (i!=0 && i%2==0) //append whitespace every 2 char
QTtext_qsPrintData+=" ";
if(i!=0 && i%16 ==0) //every 16th hex value newline
QTtext_qsPrintData+= "\n";
}
ui->textWid->setText(QTtext_qsPrintData);
//output i want is;
//FF 6C 69... <- so same hex value as in cHexData.
//25 C8 95...
//...
char * cHexData = new char [HexBuff_size+1];
memset(cHexData, '\0', HexBuff_size+1);
char * temp_iter = cHexData;
for (int i = 0; i<(HexBuff_size+1); i++){
sprintf(temp_iter ,"%X",(unsigned char)HexBuff[i]); //HexBuff is char buffer of size 256 and it holds the some data
temp_iter++; // I am printing putting the values to cHexData buffer which will hold hex values
} //ex: cHexData = FF6C69.....
//now i need to convert it to QString so that I can out the same Hex value in cHexData with formatting
for (int i = 0; i<(HexBuff_size+1); i++){
if (i!=0 && i%2==0) //append whitespace every 2 char
QTtext_qsPrintData+=" ";
if(i!=0 && i%16 ==0) //every 16th hex value newline
QTtext_qsPrintData+= "\n";
QTtext_qsPrintData+=QString(QChar(cHexData[i]));
}
ui->textWid->setText(QTtext_qsPrintData);
//output i want is;
//FF 6C 69... <- so same hex value as in cHexData.
//25 C8 95...
//...
To copy to clipboard, switch view to plain text mode
So this implementation; when in Debug mode(breakpoints) has the correct Hex value for QTtext_qsPrintData as cHexData, which is good. But in the release version, on every run, some of the Hex values in QTtext_qsPrintData changes. That in turn will display the values wrong on the output
So after reading about hex value and QString conversion; I tried the following:
...
iNumWhiteSpace = (HexBuff_size/2); //number of whitespace between 2 hex values
iNumReturns = (HexBuff_size/16)-1; //\n every 16th hex value;
for(int i = 0 ; i<HexBuff_size; i ++){
if (i!=0 && i%2==0){
*temp_iter=' ';
temp_iter++;
}
if(i!=0 && i%16 ==0){
*temp_iter='\n';
temp_iter++;
}
sprintf(temp_iter ,"%X",(unsigned char)HexBuff[i]);
temp_iter++;
}
q_bHex
= QByteArray(reinterpret_cast<char
*>
(cHexData
), HexBuff_size
+iNumWhiteSpace
+iNumReturns
+1);
//and QByteArray q_bHex(cHexData , HexBuff_size+iNumWhiteSpace+iNumReturns+1);
ui
->textWid
->setText
(QString(q_bHex
));
...
iNumWhiteSpace = (HexBuff_size/2); //number of whitespace between 2 hex values
iNumReturns = (HexBuff_size/16)-1; //\n every 16th hex value;
for(int i = 0 ; i<HexBuff_size; i ++){
if (i!=0 && i%2==0){
*temp_iter=' ';
temp_iter++;
}
if(i!=0 && i%16 ==0){
*temp_iter='\n';
temp_iter++;
}
sprintf(temp_iter ,"%X",(unsigned char)HexBuff[i]);
temp_iter++;
}
q_bHex = QByteArray(reinterpret_cast<char*>(cHexData ), HexBuff_size+iNumWhiteSpace+iNumReturns+1);
//and QByteArray q_bHex(cHexData , HexBuff_size+iNumWhiteSpace+iNumReturns+1);
ui->textWid->setText(QString(q_bHex));
To copy to clipboard, switch view to plain text mode
// This implementation is also same as above
I have also looked into something like; but the values are decimal, and that make sense since my char * buffer already have hex values.
//for (int i=0 ; i < HexBuff_size; i++)
// QTtext_qsPrintData+= QString("%1").arg(cHexData[i],0,16);
//for (int i=0 ; i < HexBuff_size; i++)
// QTtext_qsPrintData+= QString("%1").arg(cHexData[i],0,16);
To copy to clipboard, switch view to plain text mode
please help with this. I was not too sure if I should post it here or QT newbie; If I have posted it in the wrong forum please move it.
Thanks!
Bookmarks