Pregunta

I have an NSString which doesn't necessarily contain numbers, for example it could say "Abcdefghijklmnopqrstuvwxyz". I want to change this into a float value, that would be representative of this. I don't have a required formula, just some consistent way of changing this into a number value, that I can put into a randomizer.

¿Fue útil?

Solución 2

It seems the solution i'm looking for might be:

[string hash];

If if I'm particularly set on a float:

[[string hash] floatValue];

Otros consejos

You could follow the Java way of hashing strings, except using a float instead of int:

- (float)hashOfString:(NSString *str) {
    float hash = 7.0f;
    for (NSInteger i = 0; i < [str length]; i++)
        hash *= (float)[str characterAtIndex:i];
    return hash;
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top