Ivo Beckers Ivo Beckers - 1 year ago 65
Objective-C Question

Why doesn't strlen() return the correct number of bytes here?

NSString* data = @"UdmAjkmNQs79+J/a/MVVSACRkIo=";
NSData *key = [NSData dataFromBase64String:data];
const char *keyBytes = [key bytes];
NSLog(@"Length: %lu", strlen(keyBytes));

Consider the code above. I have a base64 string which contains 20 bytes of data. I turn it into bytes and I like to check the number of bytes. This prints 16. Why does it print 16 and what do I need to do to get the correct number of bytes (20)?

Answer Source

use below code to get data byte length:

NSString* data = @"UdmAjkmNQs79+J/a/MVVSACRkIo=";
NSData *key = [[NSData alloc] initWithBase64EncodedString:data options:NSDataBase64DecodingIgnoreUnknownCharacters];
NSLog(@"Length:  %lu", [key length]);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download