我無法將我設備的ID散列成64位(或更高)的表示形式,然後將其轉換爲基準表 - 31代表。任何人有任何提示或指導?我一直在網上尋找,似乎找不到多少。將設備ID散列成64位(或更高),然後將其轉換爲base-31 [iOS]
每個基-31位應該然後由該列表來表示:2 3 4 5 6 7 8 9 A B C D E F G H J K M N P Q R S T U V W X Y Z
我已經試過:
NSString *myID = [[[UIDevice currentDevice] identifierForVendor]UUIDString];
NSLog(@"Non Hash: %@", myID); //Logs the 36 character string
myID = [NSString stringWithFormat:@"%lu",(unsigned long)[myID hash]]; // Changed thanks to rokjarc
NSLog(@"Hash: %@", myID); //Logs same 36 character string
//Logs 36 character string
NSLog(@"UUIDString: %@", [[[UIDevice currentDevice] identifierForVendor] UUIDString]);
//Logs out a 10 character numeric value
NSLog(@"Hash: %lu", (unsigned long)[[[[UIDevice currentDevice] identifierForVendor] UUIDString] hash]);
//Logs out a 2 character numeric value
NSLog(@"LongLong: %lld", [[[[UIDevice currentDevice] identifierForVendor] UUIDString] longLongValue]);
使用'本身份識別碼= [本身份識別碼的散列]; ' - 這個方法不會修改它自己的字符串 - 它會返回字符串的散列... –
@rokjarc好的!它實際上是'myID = [NSString stringWithFormat:@「%lu」,(unsigned long)[myID hash]]'因爲你正在從NSUInteger轉換爲NSString。任何想法如何將其轉換爲基地31 12位數字符串?提前致謝。 – Chris
如果將問題限制爲64位整數,這並不困難。基本上保持數字除以31,並將餘數累加成一個字符串。對於大於整數的數字,事情會變得更加複雜。要麼使用大整數數學包。 – zaph