Re: NSString vs. unicode encoding

2009-09-16 Thread Johan Kool


Op 16 sep 2009, om 11:31 heeft Shawn Erickson het volgende geschreven:
On Tue, Sep 15, 2009 at 9:04 PM, Johan Kool   
wrote:

Dear list,

I need to work with strings as in stringA. (I don't have much  
choice, but to have it in a NSString at the start.) I want to have  
the readable output "hello world".


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


// This works, so I know NSString can deal with the encoding I have  
in stringA

NSLog(stringB);

// This does not work (as expected)
NSLog([NSString stringWithUTF8String:[stringA UTF8String]]);

// Nor does this work
NSLog([NSString stringWithUTF8String:[[stringA  
dataUsingEncoding:NSUTF8StringEncoding] bytes]]);


// Or this for that matter
NSLog([NSString stringWithUTF8String:[[stringA  
dataUsingEncoding:NSASCIIStringEncoding] bytes]]);


Never pass a string like that to NSLog. NSLog takes the first  
parameter as the FORMAT definition for the log statement. It parses  
that string looking for %d, etc. and then will attempt pull  
additional parameters based on what it parses. So it can cause in  
proper memory/stack access which can crash and/or expose information  
from you processes memory.


Always ensure the first parameter you pass to NSLog is a proper  
format string (this goes for any function that takes a format string).


NSLog(@"%@", ...) in this case.


Thanks, you are right. I knew that, this was just a mistake in the  
code I wrote for phrasing the question. It wasn't (nor solved) the  
issue I am having unfortunately (see my follow-up post from a few  
minutes ago).


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


Re: NSString vs. unicode encoding

2009-09-16 Thread Shawn Erickson
On Tue, Sep 15, 2009 at 9:04 PM, Johan Kool  wrote:

> Dear list,
>
> I need to work with strings as in stringA. (I don't have much choice, but
> to have it in a NSString at the start.) I want to have the readable output
> "hello world".
>
> NSString *stringA = @"hello\040world";
> NSString *stringB =  [NSString stringWithUTF8String:"hello\040world"] ;
>
> // This works, so I know NSString can deal with the encoding I have in
> stringA
> NSLog(stringB);
>
> // This does not work (as expected)
> NSLog([NSString stringWithUTF8String:[stringA UTF8String]]);
>
> // Nor does this work
> NSLog([NSString stringWithUTF8String:[[stringA
> dataUsingEncoding:NSUTF8StringEncoding] bytes]]);
>
> // Or this for that matter
> NSLog([NSString stringWithUTF8String:[[stringA
> dataUsingEncoding:NSASCIIStringEncoding] bytes]]);


Never pass a string like that to NSLog. NSLog takes the first parameter as
the FORMAT definition for the log statement. It parses that string looking
for %d, etc. and then will attempt pull additional parameters based on what
it parses. So it can cause in proper memory/stack access which can crash
and/or expose information from you processes memory.

Always ensure the first parameter you pass to NSLog is a proper format
string (this goes for any function that takes a format string).

NSLog(@"%@", ...) in this case.

-Shawn
___

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


Re: NSString vs. unicode encoding

2009-09-15 Thread Johan Kool


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   
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]);


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


Re: NSString vs. unicode encoding

2009-09-15 Thread Stephen J. Butler
On Wed, Sep 16, 2009 at 12:10 AM, Johan Kool  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.
___

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


Re: NSString vs. unicode encoding

2009-09-15 Thread Johan Kool


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".


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


Re: NSString vs. unicode encoding

2009-09-15 Thread Jens Alfke


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?



// This does not work (as expected)
NSLog([NSString stringWithUTF8String:[stringA UTF8String]]);


I would expect that to work (i.e. output "hello world".) What is the  
actual output?



// Nor does this work
NSLog([NSString stringWithUTF8String:[[stringA  
dataUsingEncoding:NSUTF8StringEncoding] bytes]]);


That is liable to misbehave or crash, since stringWithUTF8String:  
expects a null-terminated string, but -bytes doesn't produce null- 
terminated data.


—Jens___

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


NSString vs. unicode encoding

2009-09-15 Thread Johan Kool

Dear list,

I need to work with strings as in stringA. (I don't have much choice,  
but to have it in a NSString at the start.) I want to have the  
readable output "hello world".


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

// This works, so I know NSString can deal with the encoding I have in  
stringA

NSLog(stringB);

// This does not work (as expected)
NSLog([NSString stringWithUTF8String:[stringA UTF8String]]);

// Nor does this work
NSLog([NSString stringWithUTF8String:[[stringA  
dataUsingEncoding:NSUTF8StringEncoding] bytes]]);


// Or this for that matter
NSLog([NSString stringWithUTF8String:[[stringA  
dataUsingEncoding:NSASCIIStringEncoding] bytes]]);


Any suggestions for how I could do this? I haven't been able to locate  
much about this in the documentation or by searching Google.


Thanks!!

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