• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag
 

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Initializing a string with an NSData
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Initializing a string with an NSData


  • Subject: Re: Initializing a string with an NSData
  • From: Aki Inoue <email@hidden>
  • Date: Mon, 2 Jul 2001 14:59:09 -0700

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

On 2001.07.02, at 09:49, Peter Ammon wrote:

on 6/29/01 5:54 PM, Aki Inoue at email@hidden wrote:

On Friday, 6 29, 2001, at 04:53 PM, Peter Ammon wrote:

A super easy way to create an NSString from ASCII data stored in an
NSData
is

NSString* myString=[NSString stringWithCString:[myData bytes]
length:[myData
length]];

-Peter

Peter,

Unless you're absolutely sure that your software is never used by
non-English users, it is extremely dangerous to assume default C string
encoding is ASCII. For example, the code might lead to mysterious
crasher problems when executed in Japanese localization.

Aki

Interesting; I hadn't thought of this. In my case, the data I'm using to
create a string is sent from a server as ASCII, so I'm forced to interpret
it as ASCII, at least initially. Will localization affect how the
stringWithCString: method operates?

-Peter
_______________________________________________
cocoa-dev mailing list
email@hidden
http://www.lists.apple.com/mailman/listinfo/cocoa-dev


  • Follow-Ups:
    • Re: Initializing a string with an NSData
      • From: Peter Ammon <email@hidden>
References: 
 >Re: Initializing a string with an NSData (From: Peter Ammon <email@hidden>)

  • Prev by Date: Re: boolean into NSDictionary
  • Next by Date: Customized document creation
  • Previous by thread: Re: Initializing a string with an NSData
  • Next by thread: Re: Initializing a string with an NSData
  • Index(es):
    • Date
    • Thread