Op 15 sep 2009, om 23:15 heeft Johan Kool het volgende geschreven:
Op 15 sep 2009, om 22:26 heeft Stephen J. Butler het volgende geschreven:
On Wed, Sep 16, 2009 at 12:10 AM, Johan Kool <johank...@gmail.com> wrote:
Op 15 sep 2009, om 21:50 heeft Jens Alfke het volgende geschreven:
On Sep 15, 2009, at 9:04 PM, Johan Kool wrote:

NSString *stringA = @"hello\040world";
NSString *stringB = [NSString stringWithUTF8String:"hello \040world"] ;

I'm confused. '\040' is a regular ascii space character (040 = 32
decimal). What's unusual about either of these strings?

Sorry, I was confused by what the actual content of the NSString was in my app. It turned out it was @"hello\\040world". I would still like to get that
to print as "hello world" though, not "hello\040world".

Well... that's not UTF-8, for sure. You might call it ANSI C string
encoding. Looks like Foundation can handle escapes like this as
NSNonLossyASCIIStringEncoding.

Thanks! That was the nudge in the right direction. The code below gave me what I was after.

NSString *stringA = @"hello\\040world";
NSData *data = [stringA dataUsingEncoding:NSUTF8StringEncoding];
NSLog(@"%@", [[[NSString alloc] initWithData:data encoding:NSNonLossyASCIIStringEncoding] autorelease]);

Too early... turns out it only works for spaces (\040). For other special characters I still have an encoding issue, that I can't seem to work out.

NSString *stringA = @"J\\303\\270ha\\314\\212n\\040i\\314\\210s\\040Li\ \314\\200e\\314\\201\\306\\222";
NSLog(@"stringA %@ (expected Jøhån ïs Lìéƒ)", stringA);

NSData *data = [stringA dataUsingEncoding:NSASCIIStringEncoding];
NSString *stringB = [[[NSString alloc] initWithData:data encoding:NSNonLossyASCIIStringEncoding] autorelease];
NSLog(@"stringB %@ (expected Jøhån ïs Lìéƒ)", stringB);


> stringA J\303\270ha\314\212n\040i\314\210s\040Li\314\200e \314\201\306\222 (expected Jøhån ïs Lìéƒ) > stringB Jøhån ïs Lìéƒ (expected Jøhån ïs Lìéƒ)

Johan

---
http://www.johankool.nl/

KOOLISTOV - Software for Mac and iPhone
http://www.koolistov.net/




_______________________________________________

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