0

I've been reading many suggestions on the same topic, and tried to implement many of them, but it seems that none of them is actually working in my environment. I'm using QT 5, but I think the problem is not related to QT but to how the hexadecimal character 0x00 is interpreted by the language. What I have to achieve is to display a stream of unsigned char as hexadecimal values, eg:

Input bytes: 0x00 0x4E 0x01 0x00 0x17 0x00

Display as: 0x00:0x4E:0x01:0x00:0x17:0x00

it seems quite easy, but all I get is an empty string...

The functions I wrote:

QString getBufferAsHexStr(const unsigned char* buf, int buffsize) {
std::string finalstring("");
char tempbuff[5];
int n=0, index=0;
for (int c = 0; c < buffsize; c++) {
    if(c == buffsize-1) {
        n=sprintf(tempbuff, "0x%02X", buf[c]);
    } else {
        n=sprintf(tempbuff, "0x%02X:", buf[c]);
    }
    finalstring.append(tempbuff, n);
    index += n;
}
QString resultStr(finalstring.c_str());
return resultStr;
}

QString getBufferAsHexStr(const unsigned char* buf, int buffsize) {
std::stringstream ss;
for (int c = 0; c < buffsize; c++) {
    if(c == buffsize-1) {
        ss << std::hex << std::showbase << buf[c];
    } else {
        ss << std::hex << std::showbase << buf[c] << ":";
    }
}
const std::string finalstring = ss.str();
QString resultStr(finalstring.c_str());
return resultStr;
}
6
  • FIrst, why did you make tempbuff in your first example have a size equal to what you think it should be? You're walking a tightrope by making this size 5. Why not make it 10 and be safe? As a matter of fact, it is a buffer overrun, since you're trying to stuff 6 characters into it. Commented May 9, 2016 at 16:23
  • @drescherjm: Out of curiosity, why QString and not std::string? Commented May 9, 2016 at 16:25
  • That is a second option. Commented May 9, 2016 at 16:26
  • Using a properly sized buffer and replacing QString with std::string, this code shows no issue. Also, your original code with a buffer of 5 crashes the program created with Visual Studio with a corruption error (debug build). Commented May 9, 2016 at 16:34
  • 1
    @Bruno 0x%02X: You have 5 visible characters ("0x" + 2 hex characters + 1 colon) == 5) plus the terminating \0, making this 6 characters. Commented May 9, 2016 at 16:49

2 Answers 2

4

I don't know why you started to use C++ functions with C++ types when you have a much better alternative which is QString. Using QString you might implement it as follows:

QString getBufferAsHexStr(const unsigned char* buf, int buffsize) {
    QString result;
    for(int i = 0; i < buffsize; ++i)
        result += "0x" + QString("%1:").arg(buf[i], 2, 16, QChar('0')).toUpper();
    result.chop(1);
    return result;
}
Sign up to request clarification or add additional context in comments.

3 Comments

Hi ixSci, your solution works pretty fine, it just doesn't show the leading 0 when it's the case, but I can work around it. To answer your question: I don't know QT that much, I am still learning to use it properly :)
@Bruno, updated the answer. Now it handles the case with 0. Also, if the answer helps you, please click on the tick mark near the answer.
thank you very much, this is a simple matter but helped me a lot. Much appreciated
0

Another version using a QByteArray and a joined QStringList:

QString getBufferAsHexStr(QByteArray buf) {
   QStringList byteStrings;
   for (int i = 0; i < buf.size(); ++i)
      byteStrings += QString("0x") + QString("%1").arg(buf[i], 2, 16, QChar('0')).toUpper();
   return byteStrings.join(":");
}

It would by called using

QString result = getBufferAsHexStr(QByteArray(charArr, charArrSize));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.