Re: Converting C strings into NSStrings (and vice versa)
Re: Converting C strings into NSStrings (and vice versa)
- Subject: Re: Converting C strings into NSStrings (and vice versa)
- From: Kenny Leung <email@hidden>
- Date: Thu, 27 Apr 2006 08:23:29 -0700
Hi Paolo.
In order to convert from the windows encoding, I guess you just need
to make sure you've got the encoding correct. If all else fails, you
could try pre-filtering your C strings before converting them to
NSStrings. In order to convert an NSString to some other encoding
with some loss, the documnetation for cString says:
"Raises an NSCharacterConversionException if the receiver can’t be
represented in the default C-string encoding without loss of
information. Use canBeConvertedToEncoding: if necessary to check
whether a string can be losslessly converted to the default C-string
encoding. If it can’t, use lossyCString or
dataUsingEncoding:allowLossyConversion: to get a C-string
representation with some loss of information."
-Kenny
On Apr 27, 2006, at 7:38 AM, Paolo Bertani wrote:
From a text file produced on a Windows machine I generate null-
terminated c strings.
Then I need to convert those strings into NSStrings.
I have:
char *cString;
I use:
NSString *s = [NSString stringWithCString:cstring
encoding:kTextEncodingWindowsANSI];
Unfortunately if the c string contains characters like uppercase
accented U (this is how it appears on Mac, on PC it's a lowercase
accented u) the method returns nil!
I have similar troubles doing the reverse process: if some
characters cannot be represented with the given encoding the method
gives up.
For the conversion NSString -> c string now I'm using the
deprecated getCString:maxLength:
What are the correct methods I should use?
I just want to have best possible c representation of the given
NSString and vice-versa.
Thank you.
Paolo.
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
40pobox.com
This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden