Re: NSString from wstring
Re: NSString from wstring
- Subject: Re: NSString from wstring
- From: email@hidden
- Date: Tue, 4 Mar 2008 18:49:18 +0100
On 4 Mar 2008, at 18:43, Alexander Cohen wrote:
Hi,
Whats the best way to get an NSString from a wstring?
This is what I do:
@interface NSString (wstring_additions)
+(NSString*) stringWithwstring:(const std::wstring&)string;
-(std::wstring) getwstring;
@end
@implementation NSString (wstring_additions)
#if TARGET_RT_BIG_ENDIAN
const NSStringEncoding kEncoding_wchar_t =
CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingUTF32BE);
#else
const NSStringEncoding kEncoding_wchar_t =
CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingUTF32LE);
#endif
+(NSString*) stringWithwstring:(const std::wstring&)ws
{
char* data = (char*)ws.data();
unsigned size = ws.size() * sizeof(wchar_t);
NSString* result = [[[NSString alloc] initWithBytes:data length:size
encoding:kEncoding_wchar_t] autorelease];
return result;
}
-(std::wstring) getwstring
{
NSData* asData = [self dataUsingEncoding:kEncoding_wchar_t];
return std::wstring((wchar_t*)[asData bytes], [asData length] /
sizeof(wchar_t));
}
Matt
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden