Important: Please read the Qt Code of Conduct -

Porting CString formatted value to QString issue

  • Hi! I want to port CString formatted value to QString:

    DWORD dwLangCode = 0;
    CString strSubBlock = "";
    strSubBlock.Format(_T("\\StringFileInfo\\%04X%04X\\"), dwLangCode & 0x0000FFFF, (dwLangCode & 0xFFFF0000) >> 16);
    //QString example
    QString strSubBlock = QString("\\StringFileInfo\\%1%2\\").arg(dwLangCode & 0x0000FFFF).arg((dwLangCode & 0xFFFF0000) >> 16);

    I check the result with this code:

    if (VerQueryValue(lpData, (LPTSTR)(LPCTSTR)(strSubBlock.append("CompanyName").toStdWString().c_str()), &lpInfo, &unInfoLen)) {
       std::string companyName = std::wstring(reinterpret_cast<LPCTSTR>(lpInfo));
        qDebug() << QString::fromStdWString(companyName.c_str()); //returns ""

    So it returns the empty data, but when using CString it returns the actual data. Any ideas what is wrong? Thanks.

    After come checking I think my QString formatting is wrong. I get 10331200 value, but from CString I get 040904B0 value.

  • So, one option is to use: QString strSubBlock = QString().sprintf("\\StringFileInfo\\%04X%04X\\", dwLangCode & 0x0000FFFF, (dwLangCode & 0xFFFF0000) >> 16);

    But Qt docs does not recommend to use it in the new code:
    Warning: We do not recommend using QString::sprintf() in new Qt code. Instead, consider using QTextStream or arg(), both of which support Unicode strings seamlessly and are type-safe. Here's an example that uses QTextStream.

    So how to make it work with .arg() then? Thanks.

  • Lifetime Qt Champion

    If you want exactly 4 digits you should take a look in the docs - fieldWidth and fillChar are your friends.

  • @Christian-Ehrlicher

    Hi! I have tried: QString strSubBlock = QString("\\StringFileInfo\\%1%2\\").arg(dwLangCode & 0x0000FFF, 4, QChar('0')).arg((dwLangCode & 0xFFFF0000) >> 16, 4, QChar('0')); but it returns: \\StringFileInfo\\000\t000°\\
    Can you provide an example how to properly use it?

    Ok. I fixed it like this: QString strSubBlock = QString("\\StringFileInfo\\%1%2\\").arg(dwLangCode & 0x0000FFF, 4, 16, QChar('0')).arg((dwLangCode & 0xFFFF0000) >> 16, 4, 16, QChar('0'));

  • By the way, to make it uppercase:


    QString strHex = QString("%1%2").arg(dwLangCode & 0x0000FFF, 4, 16, QChar('0')).arg((dwLangCode & 0xFFFF0000) >> 16, 4, 16, QChar('0')).toUpper();
    QString strSubBlock = QString("\\StringFileInfo\\%1\\").arg(strHex);

    And now it displays value uppercase: 040904B0.

Log in to reply