Results 1 to 7 of 7

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

  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 

  4. #4
    Join Date
    Apr 2014
    Posts
    3
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11

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

    Hi ChrisW67,

    You are absolutely correct. That was not the original size. My code did do [2*size+1+other sizes] which needed me to show my other codes. So just for simplicity i did that; for sprintf I used "%.2X".


    --Back to the topic.

    This is weird. Not sure what happened...now before ChrisW67's modification i tired I Infinity's

    Qt Code:
    1. QString result;
    2. result.reserve(3 * arraySize - 1); // <------------at this line I was getting Malloc(): memory corruption:confused:
    3. for(char* i = array, end = array + arraySize; i < end; ++i) {
    4. if(!result.isEmpty()) {
    To copy to clipboard, switch view to plain text mode 

    So i removed your code Infinity's suggestion and tried ChrisW67

    Still I get this Malloc(): memory corruption.

    When i commented ChrisW67's suggestion out and make it look like before where the code worked. The memory corruption issue is still show. So i tried re-starting the machine, tired re-creating the project; no luck.

    Here is my code after the QString conversion function.
    Qt Code:
    1. tempData = QByteArray::fromRawData(cHexData , ((HexBuff_size*2)+iSizeofWhite+iSizeofRet+1) );
    2. QTtext_qsPrintData= QString(tempData.toHex());
    3.  
    4. ui->textWid->setText(QTtext_qsPrintData);
    5. delete [] cHexData ;
    6. cHexData = NULL;
    7. temp_iter= NULL;
    To copy to clipboard, switch view to plain text mode 

    If I comment out lines 1-4 I get the following:
    double free or corruption (!prev):...
    malloc(): memory corruption:...

    But it worked before.

    Thanks!


    Added after 1 44 minutes:


    Okay so i was able to fix the memory corruption issue. However, I am still having trouble with outputting hex values

    Qt Code:
    1. tempData = QByteArray::fromRawData(cHexData , ((HexBuff_size*2)+iSizeofWhite+iSizeofRet+1) );
    2. QTtext_qsPrintData= QString(tempData.toHex());
    3.  
    4. ui->textWid->setText(QTtext_qsPrintData);
    To copy to clipboard, switch view to plain text mode 

    After line 4 the values displayed in the GUI are all decimal values. If i put a breakpoint, tempData has the correct hex values. But doing tempData.toHex() converts everything to decimal. Please note: cHexData already has hex values. I want to display those values.
    Last edited by bobth12; 8th April 2014 at 20:14.

  5. #5
    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

    I've tested my code. There have been mistakes, but none of them leads to memory corruption.
    Just for completeness, that's a slightly improved version of my former draft which seems to work:
    Qt Code:
    1. static const int width = 5;
    2. QString result;
    3. result.reserve(3 * arraySize - 1);
    4. int numInLine = 0;
    5. for(unsigned char *i = array, *end = array + arraySize; i < end; ++i, ++numInLine) {
    6. if(numInLine >= width) {
    7. result += QLatin1Char('\n');
    8. numInLine = 0;
    9. }
    10. if(numInLine > 0) {
    11. result += QLatin1Char(' ');
    12. }
    13. result += QString("%1").arg(static_cast<int>(*i), 2, 16, QLatin1Char('0'));
    14. }
    To copy to clipboard, switch view to plain text mode 
    Using the toHex method of QByteArray class has the disadvantage that you don't get whitespaces and new lines, but it should work of course. I think the way you're filling your array is the problem. Show us the relevant code.
    cHexData already has hex values
    Your variable contains a number. The binary system is used to store the value internally (and to do calculations). The decimal or hexadecimal number system comes only into play when displaying the number.

  6. The following user says thank you to Infinity for this useful post:

    bobth12 (9th April 2014)

  7. #6
    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

    Please note: cHexData already has hex values. I want to display those values.
    If cHexData is a pointer to a C-style string of [0-9A-F] and whitespace chars that you simply want to display then:
    Qt Code:
    1. const char *cHexData = "51 74 20 43 65 6E 74 72 65";
    2. QString result(cHexData);
    3. // or
    4. QString result = QString::fromLatin1(cHexData);
    To copy to clipboard, switch view to plain text mode 
    or if the buffer is not NUL terminated:
    Qt Code:
    1. const char cHexData[] = {'5', '1', ' ', '7', '4', ' ', '2', '0', ' ', '4', '3', ' ', '6', '5', ' ', '6', 'E', ' ', '7', '4', ' ', '7', '2', ' ', '6', '5'};
    2. const int charCount = sizeof(cHexData);
    3.  
    4. QString result = QString::fromLatin1(cHexData, charCount);
    To copy to clipboard, switch view to plain text mode 

  8. The following user says thank you to ChrisW67 for this useful post:

    bobth12 (9th April 2014)

  9. #7
    Join Date
    Apr 2014
    Posts
    3
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11

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

    Hi,

    Thanks to both of you for the support.

    I went with:
    Qt Code:
    1. QString result = QString::fromLatin1(cHexData, charCount);
    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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.