5

I want to know the text file encoding in objective-c. Can you explain me how to know that?

Rizki
  • 281
  • 1
  • 6
  • 19

2 Answers2

7

You can use stringWithContentsOfFile:usedEncoding:error:, which returns, in addition to the new string, the encoding that was used.

I should note that this is a heuristic process by nature -- it's not always possible to determine the character encoding of a file.

Daniel Dickison
  • 21,832
  • 13
  • 69
  • 89
1

Some text document show the gibberish in my project, so I need to know the encoding of the text file, to change its encoding, let it can be read by human.

I found this : http://lists.w3.org/Archives/Public/www-validator/2002Aug/0084.html and Using OC to rewrite code,it can work for me:

    NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *sourceFilePath = [documentPath stringByAppendingPathComponent:@"fileName.txt"];
NSFileHandle *sourceFileHandle = [NSFileHandle fileHandleForReadingAtPath:sourceFilePath];
NSData *begainData = [sourceFileHandle readDataOfLength:3];

Byte *bytes = (Byte *)[begainData bytes];
if (bytes[0] == 0xff
    && bytes[1] == 0xfe
    && (begainData.length < 4
        || bytes[2] != 0
        || bytes[3] != 0
        )
    )
{
     NSLog(@"unicode");
}

if (bytes[0] == 0xfe
    && bytes[1] == 0xff
    )
     NSLog(@"BigEndianUnicode");

if (bytes[0] == 0xef && bytes[1] == 0xbb && bytes[2] == 0xbf)
    NSLog(@"UTF8");

if (bytes[0] == 0x2b && bytes[1] == 0x2f && bytes[2] == 0x76)
    NSLog(@"UTF7");

if (bytes[0] == 0xff && bytes[1] == 0xfe && bytes[2] == 0 && bytes[3] == 0)
    NSLog(@"UTF32");

if (begainData.length < 3)
    NSLog(@"ascii");
Stoull
  • 1,098
  • 8
  • 13