0

I have a C string:

unsigned char* contents = readInFile(path); //sting bytes in some unknown NSStringEncoding encoding.

I want to know the current NSStringEncoding value of contents. I want to do this without using NSString usedEncoding methods.

I need a CFStringRef from contents without introducting a new string encoding.

NSString* contentsString = [NSString stringWithCString:(char *)contents];

introduces default encoding so screws things up. How can a create a CFStringRef dirctly from contents?

Once I have this I can:

CFStringEncoding cfStringEncoding = CFStringGetFastestEncoding((CFStringRef)contentsString);
NSStringEncoding encoding = CFStringConvertEncodingToNSStringEncoding(cfStringEncoding);
rmaddy
  • 314,917
  • 42
  • 532
  • 579
ADude
  • 345
  • 5
  • 14
  • Can you read in the file as an NSData object, then use CFStringCreateWithBytes(), specifying UTF8 as the encoding? If there is a marker prefix in the data, that method probably ignores your suggestion of UTF8 and uses it instead. – David H Apr 18 '13 at 17:08
  • 1
    You can't read file into a string unless you know its encoding. Usually in this case ISO-8859-1 is used (or fragile charset detection heuristics but that's complicated), because it turns every byte into a character that maps to a Unicode code point directly from the byte's value. – Esailija Apr 19 '13 at 08:16

0 Answers0