Re: NSString and C string
Re: NSString and C string
- Subject: Re: NSString and C string
- From: Chris Ridd <email@hidden>
- Date: Wed, 19 Feb 2003 19:33:49 +0000
On 20/2/03 4:50 am, Aidas Dailide <email@hidden> wrote:
>
Hi,
>
>
My application has to do some conversion from C string to NSString and
>
backwards. The main problem is that those C strings contains characters
>
from Latin 1 and 2 encodings. It is impossible to convert NSString to C
>
string by doing cString function ,because it doesn't work. lossyCString
>
works, but it looses all characters from Latin 1 and 2 encodings. Is
>
there any way to properly convert NSString to C string and backwards
>
without loosing some characters?
>
>
Thank you,
>
>
Aidas
You have to convert your C string into an NSData, and then into an NSString.
To get it into an NSData use NSData's +dataWithBytes:length: or
+dataWithBytesNoCopy:length: or +dataWithBytesNoCopy:length:freeWhenDone:
methods.
To get the data into your NSString use NSString's -initWith
Data:encoding:
method. The encoding parameter is of type NSStringEncoding, and two valid
values of that are NSISOLatin1StringEncoding and NSISOLatin2StringEncoding.
(See NSString.h for others.)
To get the data back out again, use either NSString's -dataUsingEncoding:
method or -dataUsingEncoding:allowLossyConversion: method. The encoding
parameter is the same as above. Use NSData's -bytes method or one of the
-getBytes: methods to get an array of char..
Cheers,
Chris
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.