I’m writing a program which converts NSData to NSString with multi-byte encoding.
Conversion itself is easy but the problem is that NSData is arriving separately (The data arrives in order).
Its like
- (void) dataArrived:(NSData*) data{
NSString* mystr = [[[NSString alloc] initWithData:data encoding:NSShiftJISStringEncoding] autorelease];
// I want to parse string here and show some view as it arrives.
}
If the data contains only 8bit letters its ok but I’m parsing multi-byte string data. So what I want to do is converting NSData to NSString from head and keep the invalid bytes to next call of dataArrived (last 1 byte may be left as invalid byte for NSShiftJISStringEncoding since SJIS letter is 1 or 2 bytes).
I wonder if they have any useful method or function for doing this.
Ideal method may take NSData and Encoding and return the position where it finds invalid byte sequence in NSData.
Because the Shift JIS is encoded on 1 or 2 bytes, you can assume that:
So, we can use a
NSMutableDatabuffer and process the data like this: