5

I write a piece of code which uses both c++ and Objective C.

I need to convert NSString* to char*. [str UTF8string] wouldn't work because it returns const char*. I know:

  • std::string -> char* : char* c = const_cast<char*>(pathStr.c_str());

  • NSString* -> std::string : link

But it looks too strange and I still can't verify it. Can I convert it in a better way, for example, NSString* -> const char* -> char*

Community
  • 1
  • 1
user2083364
  • 744
  • 1
  • 7
  • 20

4 Answers4

27

Cast it to a char*

(char*)[str UTF8String];

As long as you don't want to edit it... if you do then strcpy it into some char array.

I guess it just depends on what you want to do with it.

0xFADE
  • 832
  • 5
  • 7
3

Note this:

The returned C string is a pointer to a structure inside the string object, which may have a lifetime shorter than the string object and will certainly not have a longer lifetime. Therefore, you should copy the C string if it needs to be stored outside of the memory context in which you called this method.

If you plan to modify the string, or if you plan to use it for an extended period of time, do the appropriate copy operation on it. Or use getCString:maxLength:encoding: to directly create the copy.

If you don't plan to modify the string then there's in theory no need to cast to non-const (though it is true that many interfaces call for a char* parm when they should call for const char*, so sometimes you have to cheat).

Hot Licks
  • 47,103
  • 17
  • 93
  • 151
  • I'm a newbie in C++. As I understand I create an autoreleased `NSString` object and then pass it to a function. So if I converted with `getCString:` outside the function then I reliably have `char*` variable until this function would have been finished, wouldn't it? – user2083364 Jan 04 '14 at 06:44
  • can you post a concrete example. I tried this: http://stackoverflow.com/questions/6687808/how-to-convert-nsstring-to-c-string , but it returns an empty string – user2083364 Jan 04 '14 at 06:58
1

What you are most likely experiencing is a compiler warning like:

Initializing 'char *' with an expression of type 'const char *' discards qualifiers

The compiler gives that specific warning because without the qualifier const the pointer can be derefenced and then modified. This is not just a technicality and is actually a waring that should be treated carefully.

Check out this other answer for more informations

Community
  • 1
  • 1
cescofry
  • 3,706
  • 3
  • 26
  • 22
  • `char const *` is not the same type as `char *`. And assigning the former to the latter is an error, not a warning. – Alan Stokes Jan 03 '14 at 17:08
1

My C++ is a bit rusty, but you need to decide if your string is ASCII or Unicode, then go from there. For instance:

std::string foo = std::string( [str cStringUsingEncoding: NSASCIIStringEncoding] );

If you want a Unicode string, you need to first construct a uint16_t null terminated array, and pass that to the constructor:

NSUInteger len = [str length];
uint16_t str = calloc(len+1, sizeof(uint16_t);
for(int i=0; i<len; ++i) {
  str[i] = [str characterAtIndex:i];
}
std::u16string foo = std::u16string( str );

The above should work for most Unicode strings, but if your characters are real 32bit ones, then you need to use 'wstring's using code similar to the second example.

David H
  • 40,852
  • 12
  • 92
  • 138