Results 1 to 7 of 7

Thread: issue char * hex buffer to QString conversion to print to textEdit Widget

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Apr 2014
    Posts
    3
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default issue char * hex buffer to QString conversion to print to textEdit Widget

    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!

  2. #2
    Join Date
    Oct 2012
    Posts
    132
    Thanks
    10
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: issue char * hex buffer to QString conversion to print to textEdit Widget

    Just convert each character of your C-style character array to an integer and add it to a QString:
    Qt Code:
    1. QString result;
    2. result.reserve(3 * arraySize - 1);
    3. for(char* i = array, end = array + arraySize; i < end; ++i) {
    4. if(!result.isEmpty()) {
    5. result += QLatin1Char(' ');
    6. }
    7. result += QString("%1").arg(static_cast<uint>(*i), 2, 16, QLatin1Char('0'));
    8. }
    9. hexLineEdit->setText(result);
    To copy to clipboard, switch view to plain text mode 
    That's just an quick draft. I haven't tested it.
    I recommend to avoid the usage of C-style arrays and C-style casts in C++.
    Last edited by Infinity; 7th April 2014 at 21:26.

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: issue char * hex buffer to QString conversion to print to textEdit Widget

    This code is faulty and dangerous.
    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.....
    To copy to clipboard, switch view to plain text mode 

    If you are going to convert N bytes into a hexadecimal string you need a target buffer at least 2N+1 bytes long to avoid the possibility of overrunning the buffer. In this case, your 256 source bytes require a 513 byte output buffer.

    You convert each successive byte into one (i.e. no 0 padding for values < 16) or two hexadecimal digits in the target buffer and then advance the pointer by one character. You need to consistently convert each byte to two characters ("%02X") and advance by two chars.

    If you already have the input buffer and know its size:
    Qt Code:
    1. QByteArray temp = QByteArray::fromRawData(input, inputBufferSize);
    2. QString output = QString(temp.toHex());
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 5
    Last Post: 8th September 2011, 10:38
  2. int to char conversion
    By cooper in forum Newbie
    Replies: 3
    Last Post: 14th June 2011, 03:50
  3. Char* <-> QString implicit conversion
    By kingfinn in forum Qt Programming
    Replies: 3
    Last Post: 15th March 2010, 08:26
  4. char* to QString: conversion
    By abghosh in forum Qt Programming
    Replies: 9
    Last Post: 8th March 2010, 09:32
  5. QImage from 8 bit char* RGB buffer
    By tboloo in forum Qt Programming
    Replies: 13
    Last Post: 15th April 2006, 19:56

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.