• 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: stringWithCString vs stringWithUTF8String
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: stringWithCString vs stringWithUTF8String


  • Subject: Re: stringWithCString vs stringWithUTF8String
  • From: "Clark S. Cox III" <email@hidden>
  • Date: Mon, 4 Nov 2002 13:29:04 -0500

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


On Monday, Nov 4, 2002, at 13:09 US/Eastern, Brian Redman wrote:

I've got some email text I read from a pop server. According to the header it's:

Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit

When I create an NSString from the char* text using stringWithUTF8String I get nil.

Most likely, the text of the message contains some non-ASCII, ISOLatin characters (i.e. accents, umlauts, etc.). this would make the text illegal UTF-8.

If I use stringWithCString it works. I get an NSString containing my text.

Are you sure? You may get text that is correct for all characters in the ASCII range, but I would expect that special characters would be garbled.

If I want to avoid stringWithCstring what method(s) should I be using to create the NSString from this text? I read through the NSString encoding material but I really don't get it.
Up until now replacing all my CStrings with UTF8Strings has worked transparently.

This will work only for strings that have only characters in the range of 0-127, as ASCII, UTF-8, and ISOLatin1 (as well as most other text encodings) share the 0-127 range. However, once you find a character greater than 127, the conversion will fail.

By the way the string is an email containing an ASP report saved as ascii text.

No, it's not :)
It's an email containing an ASP report saved as ISO-8859-1 text (the Content-Type header says so). The difference is important. Try this:

NSData *theData = ...; //The data containing the text to be decoded
NSString *theString = [[[NSString alloc] initWithData: theData encoding: NSISOLatin1StringEncoding] autorelease];


- -- http://homepage.mac.com/clarkcox3/
email@hidden
Clark S. Cox, III
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.7 (Darwin)

iEYEARECAAYFAj3GvHYACgkQd6STocYT1xUXgQCaA9pjo+znBQ9QeqfZR/AXPNOS
4b0An02b9rB3aGUrqJkQk9vYRiMtF2A3
=vOx4
-----END PGP SIGNATURE-----
_______________________________________________
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.

  • Follow-Ups:
    • Re: stringWithCString vs stringWithUTF8String
      • From: Brian Redman <email@hidden>
References: 
 >stringWithCString vs stringWithUTF8String (From: Brian Redman <email@hidden>)

  • Prev by Date: Re: stringWithCString vs stringWithUTF8String
  • Next by Date: Re: stringWithCString vs stringWithUTF8String
  • Previous by thread: Re: stringWithCString vs stringWithUTF8String
  • Next by thread: Re: stringWithCString vs stringWithUTF8String
  • Index(es):
    • Date
    • Thread