Re: Initializing a string with an NSData
Re: Initializing a string with an NSData
- Subject: Re: Initializing a string with an NSData
- From: Peter Ammon <email@hidden>
- Date: Tue, 03 Jul 2001 09:46:56 -0700
on 7/2/01 2:59 PM, Aki Inoue at email@hidden wrote:
>
Peter,
>
>
> Will localization affect how the stringWithCString: method operates?
>
-stringWithCString:, initWithCString:, & cString methods all assume C
>
strings are encoded in the default C string encoding.
>
You can query the encoding using +[NSString defaultCStringEncoding].
>
>
Now, the default C string encoding is set up depending on the user's
>
localization preference.
>
If the first entry in the user's localization preference is English,
>
French, German, etc, it's MacRoman encoding.
>
If it's Japanese, MacJapanese. If Arabic, MacArabic, and so on.
>
>
So, it's possible to get unexpected result from these methods depending
>
on the user's setting if you just assume it to be ASCII.
>
For example, in MacJapanese, the backslash char (ASCII 0x5C) is mapped
>
to Unicode 0xA5.
>
The following line gives you NSString containing an Unicode character
>
Yen sign (0xA5) in Japanese localization.
>
string = [NSString stringWithCString:"\"];
>
>
With multibyte languages like Japanese, Korean, Chinese, it could be
>
more fatal if the C string happen to contain a high ASCII character. In
>
these cases, the method could fail to convert from C string to Unicode,
>
and just return nil.
>
>
Aki
So is the right thing to do to use initWith
Data:myData
encoding:NSASCIIStringEncoding when the data is explicitly in ASCII
regardless of the user's localization preference?
-Peter