有个数值转换函数,JAVA和JS版都有了,本人是个刚学一个星期的OC小白,写不出OC版的数值转换函数。
下面贴出JS和JAVA版函数,望有高手们帮本人转成OC版谢谢!
JS版
下面贴出JS和JAVA版函数,望有高手们帮本人转成OC版谢谢!
JS版
function abrac_hash(str) {
var hash = 5381;
var i = str.length;
while (i) {
hash = (hash * 33) ^ str.charCodeAt(--i);
}
return hash >>> 0;
}
JAVA版
public static int getRoomID(String str) {
// >>>0 为空补0
if (null == str || "".equals(str)) {
return 0;
}
// 字符串非空,按位计算哈希
int hash = 5381;
int i = str.length();
while (i > 0) {
hash = (hash * 33) ^ str.charAt(--i);
}
if(hash<0){
hash=hash+2147483647+1;
}
LogUtils.e("getRoomID:" + hash);
return hash;
}
解决方案
40
- (int) getRoomID:(NSString*)str {
if (str.length <= 0) {
return 0;
}
int hash = 5381;
int i = (int)str.length;
while (i > 0) {
hash = (hash * 33) ^ [str characterAtIndex:--i];
}
if (hash < 0) {
hash = hash + 2147483647+1;
}
return hash;
}
不知道 对不对 你可以看下