In Objective-C, how can i decode a string that was encoded in C# using HttpServerUtility.UrlTokenEncode?
In Java, i was able to use this method suggested by @Gideon.
I tried to convert that method from Java to Objective-C and this is what i got so far.
- (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;
}
result should be a NSData
which then can be used to populate an ImageView using [UIImage imageWithData:result];
I tried to debug this method with the same data from Java method but for some reason result always becomes nil here.
I noticed in the Objective-C version, size of the base64Chars array strlen(base64Chars)
is always one less than the Java version base64Chars.length
, i tried to tweak around it but it didn't work out.
Thanks in advance for any help.