問題是NSString *unencodedString = [NSString stringWithFormat:@"%c",res]
不符合您的想法。 char res
不能保存大於128的值(對於unsigned char
,則爲256)。
char res = ((char)(400));
NSString *unencodedString = [NSString stringWithFormat:@"%c",res];
XCTAssertEqualObjects(unencodedString, @"\xc2\x90", @"");
static NSString *escape = @":/?&=;[email protected]#$()',*[]";
NSString *result = (__bridge_transfer NSString *)
CFURLCreateStringByAddingPercentEscapes(
NULL,
(__bridge CFStringRef)unencodedString,
NULL,
(__bridge CFStringRef)escape,
kCFStringEncodingUTF8);
XCTAssertEqualObjects(result, @"%C2%90", @"");
這是一個可以工作的例子。
NSString *unencodedString = @"Ɛ";
static NSString *escape = @":/?&=;[email protected]#$()',*[]";
NSString *result = (__bridge_transfer NSString *)
CFURLCreateStringByAddingPercentEscapes(
NULL,
(__bridge CFStringRef)unencodedString,
NULL,
(__bridge CFStringRef)escape,
kCFStringEncodingUTF8);
XCTAssertEqualObjects(result, @"%C6%90", @"");
UPDATE
如果你想這樣的一個樣本工作,使用unichar
和-stringWithCharacters:length:
。
unichar res = ((unichar)(400));
NSString *unencodedString = [NSString stringWithCharacters:&res length:1];
XCTAssertEqualObjects(unencodedString, @"Ɛ", @"");
static NSString *escape = @":/?&=;[email protected]#$()',*[]";
NSString *result = (__bridge_transfer NSString *)
CFURLCreateStringByAddingPercentEscapes(
NULL,
(__bridge CFStringRef)unencodedString,
NULL,
(__bridge CFStringRef)escape,
kCFStringEncodingUTF8);
XCTAssertEqualObjects(result, @"%C6%90", @"");
什麼是您的輸入,您期望輸出什麼? –
他期望輸出在所有情況下匹配@ L.B;另外,我想知道爲什麼亞歷山大是用「'417」變成「'1」的起始字符? –
輸入是帶有代碼400的字符。我希望得到url編碼的字符串'%C6%C90'。將問題解除。 – Alexander