Built-in 64-bit hash function for QString?

qHash(const QString&)returns uintwhich is 32 bit. Is there a standard Qt way to get a 64-bit hash for a string on a 32-bit system? Or do I need to implement a hash function myself?

+4
source share
2 answers

This is one way to do this. This is cross-platform, in the sense that this line will give the same hash regardless of the platform. It could be further optimized by removing the dependency on QDataStream and using byte-cutoff functions as needed to massage the content.

qint64 hash(const QString & str)
{
  QByteArray hash = QCryptographicHash::hash(
    QByteArray::fromRawData((const char*)str.utf16(), str.length()*2),
    QCryptographicHash::Md5
  );
  Q_ASSERT(hash.size() == 16);
  QDataStream stream(&hash);
  qint64 a, b;
  stream >> a >> b;
  return a ^ b;
}
+4
source

All Articles