iphone - Proper Encoding for chines character from NSData -


please me i'm using below code file name. here i'm getting nsstring nsdata multipartdata nsmutablearray contain nsdata.

nsstring* postinfo = [[nsstring alloc] initwithbytes:[[multipartdata objectatindex:1] bytes] length:[[multipartdata objectatindex:1] length] encoding:nsutf8stringencoding]; 

i'm getting string like:

printing description of postinfo:  content-disposition: form-data; name="file"; filename="??:??.png" 

but should like:

printing description of postinfo:  content-disposition: form-data; name="file"; filename="华语/華語.png" 

thanks in advance.

obviously server doesn't encode response using utf-8, another chinese-only encoding. need use content-type header detect encoding is, , find appropriate nsstringencoding using following code;

// set charset mime charset server cfstringconvertencodingtonsstringencoding(cfstringconvertianacharsetnametoencoding((__bridge cfstringref)(charset))); 

here detailed answer:

// getting content-type header (e.g. "application/json; charset=utf-8") nsstring* header = [[response allheaderfields] objectforkey:@"content-type"];  // getting mime type nsstring* charset = nil; nsarray* contenttypeparts = [header componentsseparatedbystring:@";"]; nsinteger = 0; (nsstring* part in contenttypeparts) {     // ignoring first loop (e.g. "application/json")     if (i > 0) {         nsarray* partcomponents = [part componentsseparatedbystring:@"="];         if ([partcomponents count] == 2 && [@"charset" isequaltostring:[[partcomponents objectatindex:0] stringbytrimmingcharactersinset:[nscharacterset whitespacecharacterset]]]) {             charset = [[partcomponents objectatindex:1] stringbytrimmingcharactersinset:[nscharacterset whitespacecharacterset]];             break;         }     }     i++; }  // converting mime type nsstringencoding nsstringencoding stringencoding = nsutf8stringencoding; // default utf8 if (charset) {     stringencoding = cfstringconvertencodingtonsstringencoding(cfstringconvertianacharsetnametoencoding((__bridge cfstringref)(charset))); }  // can convert string properly! nsstring* postinfo = [[nsstring alloc] initwithbytes:[[multipartdata objectatindex:1] bytes] length:[[multipartdata objectatindex:1] length] encoding:stringencoding]; 

Comments

Popular posts from this blog

java - activate/deactivate sonar maven plugin by profile? -

python - TypeError: can only concatenate tuple (not "float") to tuple -

java - What is the difference between String. and String.this. ? -