1
\$\begingroup\$

I wrote a function to convert a hexadecimal string representation (like 0x00) of some binary data to the data itself.

How can I improve this code?

QByteArray restoreData(const QByteArray &data, const QString prepender = "x")
{
    QByteArray restoredData = data;

    return QByteArray::fromHex(restoredData.replace(prepender, ""));
}
\$\endgroup\$
  • 1
    \$\begingroup\$ Why you need to improve this code? If you really need to do this, try to find way to create QByteArray without copying source data... \$\endgroup\$ – k06a May 25 '14 at 12:53
1
\$\begingroup\$

Try to avoid deep copying of source array, this can be achived with right method:

QByteArray restoreData(const QByteArray &data, char prepender = 'x')
{
    return QByteArray::fromHex(data.right(data.size() - data.indexOf(prepender) - 1);
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.