• 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
[Q] CFStringGetCStringPtr( ..., kCFStringEncodingUTF8)
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Q] CFStringGetCStringPtr( ..., kCFStringEncodingUTF8)


  • Subject: [Q] CFStringGetCStringPtr( ..., kCFStringEncodingUTF8)
  • From: JongAm Park <email@hidden>
  • Date: Tue, 23 Jun 2009 10:26:01 -0700

Hello.

I didn't find any CoreFoundation mailing list, so I post my question about CFStringEtCStringPtr() here in Cocoa mailing list.

My problem is that CFStringGetCStringPtr( ..., kCFStringEncodingUTF8) doesn't convert a string passed through its first parameter and returns 0 on English system.
However, on Japanese system, it returns a pointer to converted string correctly.


The first parameter points to a string which is a path in English. There is no Japanese characters.
Because the 2nd parameter is one of 8 bit encoding, it didn't work before on Japanese system, where it was called like :


CFStringGetCStringPtr( ...,  CFStringGetSystemEncoding() )

The encoding method on a Japanese system is

kCFStringEncodingMacJapanese <http://developer.apple.com/documentation/CoreFoundation/Reference/CFStringRef/Reference/reference.html#//apple_ref/doc/c_ref/kCFStringEncodingMacJapanese>

So, I forced the 2nd parameter to kCFStringEncodingUTF8 and thought that it would work on English system also.
However, on an English system, of which system encoding is


kCFStringEncodingMacRoman <http://developer.apple.com/documentation/CoreFoundation/Reference/CFStringRef/Reference/reference.html#//apple_ref/doc/c_ref/kCFStringEncodingMacRoman>


It doesn't convert its 1st parameter and returned 0.
So, I had to call another CFStringGetCStringPtr() with CFStringGetSystemEncoding() as its 2nd parameter.


Is it a bug? Usually English encoding is the most trouble-free encoding method which can be converted to UTF8. So, I expected that it would work with kCFStringEncodingUTF8.
Or.. can anyone tell me why it doesn't work?


My whole source code which contains this fix is like

char *fullPath;
char outPath[512];;
Boolean conversionResult;
CFStringEncoding encodingMethod;
// This is for ensuring safer operation. When CFStringGetCStringPtr() fails,
// it tries CFStringGetCString().
encodingMethod = CFStringGetSystemEncoding();
// 1st try for English system
fullPath = (char*)CFStringGetCStringPtr(mstr, encodingMethod);
if( fullPath == NULL )
{
// 2nd try for Japanese system
encodingMethod = kCFStringEncodingUTF8;
fullPath = (char*)CFStringGetCStringPtr(mstr, encodingMethod);
}
// for safer operation.
if( fullPath == NULL )
{
CFIndex length = CFStringGetLength(mstr);
fullPath = (char *)malloc( length + 1 );
conversionResult = CFStringGetCString(mstr, fullPath, length, kCFStringEncodingUTF8 );
strcpy( outPath, fullPath );
free( fullPath );
}
else
strcpy( outPath, fullPath );


Thank you.

_______________________________________________

Cocoa-dev mailing list (email@hidden)

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:
This email sent to email@hidden


  • Follow-Ups:
    • Re: [Q] CFStringGetCStringPtr( ..., kCFStringEncodingUTF8)
      • From: Laurent Cerveau <email@hidden>
  • Prev by Date: Re: Realtime, GC and ObjC (Was Re: Opinion on managed memory and garbage collection)
  • Next by Date: Re: Problem: how to get glyph descent and ascent
  • Previous by thread: Re: Problem: how to get glyph descent and ascent
  • Next by thread: Re: [Q] CFStringGetCStringPtr( ..., kCFStringEncodingUTF8)
  • Index(es):
    • Date
    • Thread