Reputation: 2824
I am trying to convert NSString
to C-String` using the following call:
NSString* path = ...
const char* str = [path cStringUsingEncoding: NSUTF16StringEncoding];
The path
contains a file path. But, I am getting str as just "/". When I use this call instead:
const char* str = [path UTF8String];
The returned str
is fine i.e. I get the required path as a c-string. But, I need to make sure that the conversion works in all cases regardless of the the type encoding used in the path i.e. I want to take care of unicode characters. And for that I need to use the initial call.
What mistake am I making?
Upvotes: 1
Views: 252
Reputation: 1286
You should try using UTF8 encoding this '\' you get will be the first of the NSString...in c each char takes 1 byte (8bits) but UTF16 makes characters 2 bytes long.. and so the error occurs
Upvotes: 0
Reputation: 122401
It's because of the encoding you chose. UTF-16 will return the characters in 16-bit and you are expecting 8-bit characters (i.e. UTF-8).
So if the path starts with /
, that will be returned as 0x2F00
(assuming little-endian encoding) and the 00
sequence will look end of string.
Upvotes: 3