简体   繁体   中英

Base64 string Decoded to NSData and encoding to UTF-8

I get a base64 encoded string from out servers. I decode it to base64:

DLog(@"parseing string to data: %@", value);

NSData *decodedData = [[NSData alloc] initWithBase64EncodedString:value options:0];
DLog(@"decoded value: %@", decodedData);

NSString *encodedString = [[NSString alloc] initWithData:decodedData encoding:NSUTF8StringEncoding];
DLog(@"encodedString: %@", encodedString);

Log:

parseing string to data: UEsDBBQAAAAIACmEAUlpoChO7hYAAIHNBgAGABwAc20uZGJmVVQJAAMdwJ9XHcCfV3V4CwABBOcDAAAE5wMAAO2dzaokxxFGr5AX9sLv0N61wRSVWf...>

decoded value: <504b0304 14000000 08002984 014969a0 284eee16 000081cd 06000600 1c00736d 2e646266 55540900 031dc09f 571dc09f 5775780b 000104e7 03000004 e7030000...>

encodedString: (null)

But my sting is always null.

Why could this be happening?

The initWithData documentation says:

Returns nil if the initialization fails for some reason (for example if data does not represent valid data for encoding).

Try to begin with a valid string, convert it into data, print it to the log and compare vs. the data that you get from initWithBase64EncodedString.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM