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, ""));
}
Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked May 24, 2014 at 8:27
\$\endgroup\$
1
  • 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\$ Commented May 25, 2014 at 12:53

1 Answer 1

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);
}
answered May 25, 2014 at 12:59
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.