I have an App that reads data from a server. Now and then, the data appears to be not valid UTF-8. If I convert from the byte array to an UTF8-String, the string appears nil. There must be some invalid not-UTF8 character in the byte array. Is there a way to 'lossy' convert the byte array to UTF8 and filter out only the invalid characters?
Any ideas?
My code looks like this:
- (void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent {
switch (streamEvent){
case NSStreamEventHasBytesAvailable:
{
uint8_t buffer[1024];
int len;
NSMutableData * inputData = [NSMutableData data];
while ([directoryStream hasBytesAvailable]){
len = [directoryStream read:buffer maxLength:sizeof(buffer)];
if (len> 0) {
[inputData appendBytes:(const void *)buffer length:len];
}
}
NSString *directoryString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
}
NSLog(@"directoryString: %@", directoryString);
...
Is there a way to do this conversion in a more 'lossy' way?
As you see I first append the chunks of data to an NSData value and do the conversion to utf8 when everything is read. This prevents that the (multi-byte) utf8 characters are split up resulting in even more invalid (empty) utf8 strings.