groups

Link to this snippet:


Download to Code Collector

language: Objective-C
licence: Other

FourCharCode to NSString

options: send to code collectorview all myztikjenz's snippets
// Two extensions to covert the FourCharCode values used in various places into a string and back again

@interface NSString (NSString-Extensions)
+ (NSString *) stringFromOSType:(unsigned long)type;
-(unsigned long) OSType;
@end

@implementation NSString (NSString-Extensions)
+ (NSString *) stringFromOSType:(unsigned long)type
{
	SInt32 hardwareType;
	Gestalt( gestaltSysArchitecture, &hardwareType );
	
	UInt32 _type = type;
	if( hardwareType == gestaltIntel )
		_type = Endian32_Swap( type );
	
	NSData *data = [NSData dataWithBytes:&_type length:sizeof( _type )];
	return( [[[NSString alloc] initWithData:data encoding:NSMacOSRomanStringEncoding] autorelease] );
}

-(unsigned long) OSType
{
	NSData *stringData = [self dataUsingEncoding:NSMacOSRomanStringEncoding];
	
	UInt32 type, _type;
	[stringData getBytes:&type];

	SInt32 hardwareType;
	Gestalt( gestaltSysArchitecture, &hardwareType );

	_type = type;
	if( hardwareType == gestaltIntel )
		_type = Endian32_Swap( type );
	
	return( _type );
}
@end

@interface NSNumber (NSNumber-Extensions)
-(NSString *) fourCharCodeString;
@end

@implementation NSNumber (NSNumber-Extensions)
-(NSString *) fourCharCodeString
{
	unsigned long type = [self unsignedLongValue];

	SInt32 hardwareType;
	Gestalt( gestaltSysArchitecture, &hardwareType );
	
	UInt32 _type = type;
	if( hardwareType == gestaltIntel )
		_type = Endian32_Swap( type );
	
	NSData *data = [NSData dataWithBytes:&_type length:sizeof( _type )];
	return( [[[NSString alloc] initWithData:data encoding:NSMacOSRomanStringEncoding] autorelease] );
}
@end