On Apr 12, 2009, at 9:21 PM, Michael Ash wrote:

On Sun, Apr 12, 2009 at 6:01 PM, John Joyce
<dangerwillrobinsondan...@gmail.com> wrote:
Is there an easy way to take input (user or file based) at runtime and convert unicode strings such as \u8D64 (UTF8 character) or a whole series of
these to the human-readable characters they represent?
I imagine I should be using NSScanner, but is there not some simple function or method to lookup and return the character as it should be represented? Happy to RTFM, just need a pointer to the docs I should be looking at.

Note that those are just unicode code points, not UTF-8 "characters",
whatever that would be. There is nothing built-in that I know of to
convert these.

You can search the string using one of the built-in methods, and then
read the number with NSScanner's -scanHexInt:. If you know that your
code points will always be no more than 4 digits (16 bits), which I
think would have to be the case because otherwise you wouldn't know
where to stop scanning the number, then you can make a string out of
it easily using the %C format specifier, like so:

unsigned value;
if([scanner scanHexInt:&value]) {
   NSString *str = [NSString stringWithFormat:@"%C", value];
   ... put str back into the original string ...
}
else // couldn't parse the value

The above is failing to take into account the assumed knowledge that the code points won't be more than 4 characters long. What if you follow a \uNNNN sequence with characters (not intended to be part of the \u sequence) in the [0-9a-fA-F] set? For example, "bar \u0020foo". -scanHexInt: will take that to mean 0x20f. You'd need to -scanCharactersFromSet:intoString: to read a sequence of hexadecimal digits, then check the length and back up using -setScanLocation: if it was more than 4 digits. Also, you'd truncate the resulting string at 4 digits and scan _that_ using -scanHexInt:.

Regards,
Ken

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to