Question: Following java code returns hash code of a string. 

```
String uri = "Some URI"
public int hashCode() {
    return uri.hashCode();
}

```

I want to translate this code to c++. Is there any function availabe in c++ or an easy way to translate this.

Answer: 
The following is the source for the default `String.hashCode()` in Java, this is a trival exercise to implement in C++.

```
public int hashCode()  
{
       int h = hash;
       if (h == 0 && count > 0) 
       {
           int off = offset;
           char val[] = value;
           int len = count;

           for (int i = 0; i < len; i++) 
           {
               h = 31*h + val[off++];
           }
           hash = h;
       }
       return h;
   }

```
//For C++ Qt you can use this code, the result is the sames as for Java hashcode()

```
int  hashCode(QString text){
    int hash = 0, strlen = text.length(), i;
    QChar character;
    if (strlen == 0)
        return hash;
    for (i = 0; i < strlen; i++) {
        character = text.at(i);
        hash = (31 * hash) + (character.toAscii());
    }
    return hash; 
}

```