• 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
Converting web data to Strings - taking into account the original encoding
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Converting web data to Strings - taking into account the original encoding


  • Subject: Converting web data to Strings - taking into account the original encoding
  • From: Scott Anguish <email@hidden>
  • Date: Sat, 13 Sep 2003 05:07:49 -0400

On Sep 13, 2003, at 4:14 AM, John Lombardo wrote:

I've only ever asked questions here after having spent hours searching,
experimenting, and being totally stumped on a problem... And this time, like
every other time, moments after clicking "Send Now" in Entourage I happened
get an idea (or notice a detail, or inadvertently spot something relevant in
AppKiDo) that solves the problem...

This time the solution is:
[NSString stringWithCString:[newBytes bytes] length:[newBytes length]]


That's a solution, but not necessarily the best one.

You're assuming in this case that the encoding of the incoming string is ASCII. That may not be the case. And if you hit a page that has a different encoding, birds will fall from the sky and life as we know it on Earth will cease. Seriously... (by which I mean, not seriously.. about the birds and Earth)

Depending on what you're trying to do, you may want to look instead at the NSString method -initWithData:encoding:

That method allows you to pass the NSData object, and an NSStringEncoding.

That does lead to some issues mind you. First, you need to decide what you want to use as the string encoding. And to do that you really need to know what the original source you downloaded from was encoded using.

Using URL Loading you can get encoding by using NSURLConnection/NSURLDownload to download the data, and then looking at the NSURLResponse that is returned, passing it a -textEncodingName message. This returns a string that can be converted to an NSStringEncoding. Then you could use that to convert the data that you get from the download to a string. And then all is right with the world.

That sounds complicated. But I think that if you can work sync, then using NSURLConnection +sendSynchronousRequest:returningResponse:error: you can probably create an NSURLConnection category method that returns a string for URL with the proper encoding in, oh, less than 6 lines.

To use the NSURLConnection and NSURLDownload classes and their associates, you need to depend on running on Mac OS X 10.2.6 with Safari 1.0 installed, or a later version of the OS.
_______________________________________________
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.
References: 
 >EVERY SINGLE FREAKIN TIME!! (Was NSData to NSString following resourceDataDidBecomeAvailable:) (From: John Lombardo <email@hidden>)

  • Prev by Date: Re: Extracting info from notification
  • Next by Date: Re: Extracting info from notification
  • Previous by thread: EVERY SINGLE FREAKIN TIME!! (Was NSData to NSString following resourceDataDidBecomeAvailable:)
  • Next by thread: How do I debug an autorelease crash?
  • Index(es):
    • Date
    • Thread