Re: NSString from wstring
Re: NSString from wstring
- Subject: Re: NSString from wstring
- From: Alexander Cohen <email@hidden>
- Date: Tue, 04 Mar 2008 13:31:07 -0500
thx, works like a charm.
AC
On 4-Mar-08, at 12:49 PM, email@hidden wrote:
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