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:
Qt Code:
  1. char * cHexData = new char [HexBuff_size+1];
  2. memset(cHexData, '\0', HexBuff_size+1);
  3. char * temp_iter = cHexData;
  4.  
  5. for (int i = 0; i<(HexBuff_size+1); i++){
  6. sprintf(temp_iter ,"%X",(unsigned char)HexBuff[i]); //HexBuff is char buffer of size 256 and it holds the some data
  7. temp_iter++; // I am printing putting the values to cHexData buffer which will hold hex values
  8. } //ex: cHexData = FF6C69.....
  9.  
  10.  
  11. //now i need to convert it to QString so that I can out the same Hex value in cHexData with formatting
  12. for (int i = 0; i<(HexBuff_size+1); i++){
  13.  
  14. if (i!=0 && i%2==0) //append whitespace every 2 char
  15. QTtext_qsPrintData+=" ";
  16.  
  17. if(i!=0 && i%16 ==0) //every 16th hex value newline
  18. QTtext_qsPrintData+= "\n";
  19.  
  20. QTtext_qsPrintData+=QString(QChar(cHexData[i]));
  21. }
  22.  
  23. ui->textWid->setText(QTtext_qsPrintData);
  24.  
  25. //output i want is;
  26. //FF 6C 69... <- so same hex value as in cHexData.
  27. //25 C8 95...
  28. //...
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:

Qt Code:
  1. ...
  2. iNumWhiteSpace = (HexBuff_size/2); //number of whitespace between 2 hex values
  3. iNumReturns = (HexBuff_size/16)-1; //\n every 16th hex value;
  4.  
  5. for(int i = 0 ; i<HexBuff_size; i ++){
  6. if (i!=0 && i%2==0){
  7. *temp_iter=' ';
  8. temp_iter++;
  9. }
  10. if(i!=0 && i%16 ==0){
  11. *temp_iter='\n';
  12. temp_iter++;
  13. }
  14.  
  15. sprintf(temp_iter ,"%X",(unsigned char)HexBuff[i]);
  16. temp_iter++;
  17.  
  18. }
  19.  
  20. q_bHex = QByteArray(reinterpret_cast<char*>(cHexData ), HexBuff_size+iNumWhiteSpace+iNumReturns+1);
  21. //and QByteArray q_bHex(cHexData , HexBuff_size+iNumWhiteSpace+iNumReturns+1);
  22. 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.
Qt Code:
  1. //for (int i=0 ; i < HexBuff_size; i++)
  2. // 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!