Skip to content

Instantly share code, notes, and snippets.

@kim4apple
Last active May 16, 2018 06:20
Show Gist options
  • Save kim4apple/da9bbf34a1627b76d20510b4472c710f to your computer and use it in GitHub Desktop.
Save kim4apple/da9bbf34a1627b76d20510b4472c710f to your computer and use it in GitHub Desktop.
ignore the invalid utf-8
- (NSData *)cleanUTF8:(NSData *)data {
// this function is from
// https://stackoverflow.com/questions/3485190/nsstring-initwithdata-returns-null
//
//
iconv_t cd = iconv_open("UTF-8", "UTF-8"); // convert to UTF-8 from UTF-8
int one = 1;
iconvctl(cd, ICONV_SET_DISCARD_ILSEQ, &one); // discard invalid characters
size_t inbytesleft, outbytesleft;
inbytesleft = outbytesleft = data.length;
char *inbuf = (char *)data.bytes;
char *outbuf = malloc(sizeof(char) * data.length);
char *outptr = outbuf;
if (iconv(cd, &inbuf, &inbytesleft, &outptr, &outbytesleft)
== (size_t)-1) {
NSLog(@"this should not happen, seriously");
return nil;
}
NSData *result = [NSData dataWithBytes:outbuf length:data.length - outbytesleft];
iconv_close(cd);
free(outbuf);
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment