什么是C#' HttpServerUtility.UrlTokenDecode的Objective-C等价物?

时间:2015-05-23 12:07:47

标签: objective-c

在Objective-C中,如何使用HttpServerUtility.UrlTokenEncode解码用C#编码的字符串?

在Java中,我能够使用@Gideon建议的this method

我尝试将该方法从Java转换为Objective-C,这是我到目前为止所做的。

- (NSData *)URLTokenDecode:(NSString *)input {

NSData *result;
NSUInteger len = [input length];
NSUInteger numPadChars = (int)[input characterAtIndex:len-1] - (int)'0';

if(numPadChars >10)
    return nil;

char base64Chars[len - 1 + numPadChars];

for(int iter = 0; iter < len-1 ; iter++){
    char c = [input characterAtIndex:iter];
    switch (c) {
        case '-':
            base64Chars[iter] = '+';
            break;

        case '_':
            base64Chars[iter] = '/';
            break;

        default:
            base64Chars[iter] = c;
            break;
    }
}

for(int iter = len-1; iter < strlen(base64Chars); iter++){
    base64Chars[iter] = '=';
}

NSString* assembledString = [NSString stringWithCString:base64Chars encoding:NSASCIIStringEncoding];
result = [[NSData alloc] initWithBase64EncodedString:assembledString options:0];

return result;
}

结果应该是NSData,然后可以使用[UIImage imageWithData:result];来填充ImageView

我尝试使用Java方法中的相同数据调试此方法,但由于某种原因,结果总是在这里变为零。

我注意到在Objective-C版本中,base64Chars数组strlen(base64Chars)的大小总是比Java版本base64Chars.length小一个,我试图调整它但它没有工作进行。

提前感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

使用strlen()来获取长度,它是已经填充的长度,而不是base64Chars的大小,也不是java实现中的len - 1 + numPadChars。所以=填充可能无法填充。

你可以尝试我的固定方法:

- (NSData *)UrlTokenDecode:(NSString *)input {

NSData *result;
NSUInteger len = [input length];
NSUInteger numPadChars = (int)[input characterAtIndex:len-1] - (int)'0';

if(numPadChars >10)
    return nil;

char* base64Chars = malloc(len+numPadChars); // no -1 because need a '\0' end
memset(base64Chars, 0, len+numPadChars);

for(int iter = 0 ; iter < len-1 ; iter++){
    char myChar = [input characterAtIndex:iter];
    switch (myChar) {
        case '-':
            base64Chars[iter] = '+';
            break;

        case '_':
            base64Chars[iter] = '/';
            break;

        default:
            base64Chars[iter] = myChar;
            break;
    }
}

for(int iter = len-1 ; iter < len+numPadChars-1; iter++){
    base64Chars[iter] = '=';
}

NSString* assembledString = [NSString stringWithCString:base64Chars encoding:NSASCIIStringEncoding];
result = [[NSData alloc] initWithBase64EncodedString:assembledString options:0];
free(base64Chars);

return result;
}

答案 1 :(得分:0)

作为替代方法。我使整个方法更加符合Objective-C。

NSData *URLTokenDecode(NSString *token)
{
    if (token.length == 0) return nil;

    // The last character in the token is the number of padding characters.
    NSUInteger numberOfPaddingCharacters = [token characterAtIndex:token.length - 1] - '0';
    if (numberOfPaddingCharacters > 10) return nil;

    // The Base64 string is the token without the last character.
    NSString *base64 = [token substringWithRange:NSMakeRange(0, token.length - 1)];

    // '-'s are '+'s and '_'s are '/'s.
    base64 = [base64 stringByReplacingOccurrencesOfString:@"-" withString:@"+"];
    base64 = [base64 stringByReplacingOccurrencesOfString:@"_" withString:@"/"];

    // Pad the Base64 string out with '='s
    NSUInteger paddedLength = base64.length + numberOfPaddingCharacters;
    base64 = [base64 stringByPaddingToLength:paddedLength withString:@"=" startingAtIndex:0];

    NSData *result = [[NSData alloc] initWithBase64EncodedString:base64 options:kNilOptions];
    return result;
}