iOS-文字转码 形成搜索链接的时候,把从textField获取的文字转码放入。。 #pragma mark 转码 ///// - (NSString*) changeEncoding : (NSString*) str //转换成 encoding编码 { if (str == nil || [str isEqualToString : @""]) { return str; } // NSData* str2 = [str dataUsingEncoding : NSUTF8StringEncoding]; //NSString* str2 = [ NSString stringWithUTF8String : [str UTF8String] ]; const char* char2 = [str UTF8String]; int caseDiff = (‘a‘ - ‘A‘); NSMutableString* out1 = [NSMutableString string]; //StringBuffer out = new StringBuffer(s.length()); for ( ; *char2 != ‘\0‘ ; char2++) { // out.append(‘%‘); [out1 appendString : @"%"]; char ch = [ self forDigit : ( *char2 >> 4) & 0xF : 16 ]; if (![self isDigit : ch] ) { ch -= caseDiff; } NSString* tmp = [[NSString alloc] initWithFormat : @"%c", ch ]; [out1 appendString : tmp]; // out.append(ch); ch = [ self forDigit : *char2 & 0xF : 16 ]; ch -= caseDiff; NSString* tmp1 = [[NSString alloc] initWithFormat : @"%c", ch ]; [out1 appendString : tmp1]; // out.append(ch); } // return out.toString(); return out1 ; } - (BOOL) isDigit : (char) chr { if (chr <= ‘9‘ && chr >= ‘0‘) { return YES; }else { return NO; } } - (char) forDigit : (int) digit : (int) radix { if ((digit >= radix) || (digit < 0)) { return ‘\0‘; } if ((radix < 2) || (radix > 36)) { return ‘\0‘; } if (digit < 10) { return (char) (‘0‘ + 32 + digit); } return (char) (‘a‘ - 10 + digit); }
时间: 2024-11-08 21:24:42