1

I am reading my app directory like this

NSArray *pathss = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectorys = [pathss objectAtIndex:0];
NSError * error;
NSMutableArray * directoryContents =  [[NSFileManager defaultManager]
                            contentsOfDirectoryAtPath:documentsDirectorys error:&error];

the output i get:

"Forms_formatted.pdf",
"fund con u\U0308u\U0308.pdf",
"hackermonthly-issue.pdf",

these are the files name. my question, how come i able to convert this name "fund con u\U0308u\U0308.pdf" to correct format. thanks in advance

  • 1
    There **is no problem**. It's just the (strange) way that NSArray uses to print non-ASCII characters. If you access the individual elements e.g. `directoryContents[1]` then everything will be OK. Compare http://stackoverflow.com/questions/16774544/saving-hebrew-text-to-nsuserdefaults-return-strange-encoding. – Martin R Jan 09 '15 at 12:41
  • 1
    actually this word is "fund con ü.pdf". I want it to print same instead of this "fund con u\U0308u\U0308.pdf". – andrea smith Jan 14 '15 at 08:23
  • And what output to you get for `NSLog(@"file=%@", directoryContents[1]);` ? – Martin R Jan 14 '15 at 08:47

3 Answers3

0

Convert the NSString to NSData with UTF-16 encoding, convert back to a NSString with UTF-8 encoding. It is probably UTF-16 big endian with no BOM. The character is not ASCII.

The character u\U0308 is not ASCII, it is a Combining Diacritical Mark ̈ü, the character is also known as double dot above, umlaut, Greek dialytika and double derivative. The \U0308 puts the umlaut above the u character.

zaph
  • 111,848
  • 21
  • 189
  • 228
0

Maybe this will help:

NSString *documentsDirectorys = [pathss objectAtIndex:0];
const char *documentsDirectoryCstring = [documentsDirectorys cStringUsingEncoding:NSASCIIStringEncoding];

EDIT:

const char *documentsDirectoryCstring = [documentsDirectorys cStringUsingEncoding:NSUTF8StringEncoding];
jwlaughton
  • 905
  • 1
  • 6
  • 11
  • The u\U0308 character is not ASCII, it is a combining character, it can not be represented in ASCII. It is a UTF-16 combining character. – zaph Jan 09 '15 at 12:31
-1

You can try this. NSLog use description method of NSArray to print NSArray object, which deals with unicode characters differently than the description on objects its contains such as NSString objects. Or you can loop through the array and NSLog each item.

NSLog(@"%@", [NSString stringWithCString:[[array description] cStringUsingEncoding:NSASCIIStringEncoding] encoding:NSNonLossyASCIIStringEncoding]);
gabbler
  • 13,626
  • 4
  • 32
  • 44