• 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: Translating UTF8 range into NSString range?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Translating UTF8 range into NSString range?


  • Subject: Re: Translating UTF8 range into NSString range?
  • From: "Louis C. Sacha" <email@hidden>
  • Date: Wed, 6 Oct 2004 03:44:31 -0700

Hello...

If you have access to the UTF8 bytes, you can probably use the NSString initializer initWithBytes:length:encoding: to calculate the equivalent range in an NSString.

http://developer.apple.com/documentation/Cocoa/Reference/Foundation/ObjC_classic/Classes/NSString.html#//apple_ref/doc/uid/20000154/BCIGDAHA

(untested, typed in mail, etc...)


char utf8bytes[]; /* the complete array of UTF8 bytes */
NSRange utf8SubstringRange; /* a valid range in the array of UTF8 bytes */

NSString *locationString = [[NSString alloc] initWithBytes:utf8bytes length:utf8SubstringRange.location encoding:NSUTF8StringEncoding];
NSString *lengthString = [[NSString alloc] initWithBytes:(void *)(utf8bytes + utf8SubstringRange.location) length:utf8SubstringRange.length encoding:NSUTF8StringEncoding];

NSRange unicodeSubstringRange;
unicodeSubstringRange.location = [locationString length];
unicodeSubstringRange.length = [lengthString length];

[locationString release];
[lengthString release];


You will probably want to add some error checking to make sure that the NSStrings don't come back as nil, since that would result in an incorrect range.

It might not be very efficient, but it is probably fast enough unless you need to process a huge number of these ranges or the size of the UTF8 data is very large.


Hope that helps,

Louis


Hello!

I have an array of utf8 encoded bytes, which are processed by flex (it works in 8-bit). This processing results in ranges of characters, but for the utf8 byte array. I would like to have these ranges in Unicode range, so I can use them with NSString, which is created from this utf8 encoded data (characterAtIndex: and so on)...

These utf8 ranges are known to be valid, because all non-ascii stuff is grouped together, so no Unicode character is ever split.

Does anybody know of a way to translate utf8 range into corresponding NSString Unicode range?


Regards,
izidor

_______________________________________________
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
References: 
 >Translating UTF8 range into NSString range? (From: Izidor Jerebic <email@hidden>)

  • Prev by Date: Re: [OT] help please, trying to find Louis Sacha
  • Next by Date: Re: force entire window redraw?
  • Previous by thread: Translating UTF8 range into NSString range?
  • Next by thread: Help With NSURLConnection Authentication
  • Index(es):
    • Date
    • Thread