Return to Snippet

Revision: 3875
at November 4, 2007 09:46 by 0xced


Updated Code
// http://www.omnigroup.com/ftp/pub/software/Source/MacOSX/Frameworks/
// Link with {OWF, OmniFoundation, OmniBase, OmniNetworking}
// http://forums.omnigroup.com/showthread.php?t=1867
#import <OWF/OWDataStream.h>
#import <OWF/OWDataStreamCharacterCursor.h>

NSString* stringWithUTF8Data(NSData *data)
{
	OWDataStream *dataStream = [[[OWDataStream alloc] initWithLength:[data length]] autorelease];
	[dataStream writeData:data];
	return OFMostlyApplyDeferredEncoding([[[[OWDataStreamCharacterCursor alloc] initForDataCursor:[dataStream newCursor] encoding:OFDeferredASCIISupersetStringEncoding] autorelease] readString], kCFStringEncodingUTF8);
}

Revision: 3874
at September 26, 2007 07:17 by 0xced


Initial Code
// http://www.omnigroup.com/ftp/pub/software/Source/MacOSX/Frameworks/
// Link with {OWF, OmniFoundation, OmniBase, OmniNetworking}
#import <OWF/OWDataStream.h>
#import <OWF/OWDataStreamCharacterCursor.h>

NSString* stringWithUTF8Data(NSData *data)
{
	OWDataStream *dataStream = [[[OWDataStream alloc] initWithLength:[data length]] autorelease];
	[dataStream writeData:data];
	return OFMostlyApplyDeferredEncoding([[[[OWDataStreamCharacterCursor alloc] initForDataCursor:[dataStream newCursor] encoding:OFDeferredASCIISupersetStringEncoding] autorelease] readString], kCFStringEncodingUTF8);
}

Initial URL

                                

Initial Description
Convert UTF-8 NSData to NSString without failing if UTF-8 is invalid. Instead, replace invalid characters with the replacement character (�). This requires some Omni frameworks. Use this as a replacement for NSString's initWithData:encoding: method.

Initial Title
Handle invalid UTF-8

Initial Tags

                                

Initial Language
Objective C