It seems the solution i'm looking for might be:
[string hash];
If if I'm particularly set on a float:
[[string hash] floatValue];
문제
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.
해결책 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];
다른 팁
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;
}