Question

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.

Was it helpful?

Solution 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];

OTHER TIPS

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;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top