Solved

How do i detect the encoding of a text file?

Posted on 2011-10-01
5
1,546 Views
Last Modified: 2012-06-21
How do i detect the encoding of a text file?
Is there a way to automatically detect encoding of a file?
NSString *txtFile = [NSString stringWithContentsOfFile:newFilePath
encoding: ??? I don't know encoding
error:nil];

Open in new window

0
Comment
Question by:jacekmalinowski
  • 3
  • 2
5 Comments
 
LVL 33

Expert Comment

by:pgnatyuk
ID: 36899462
0
 
LVL 4

Author Comment

by:jacekmalinowski
ID: 36900387
Thanks for your reply.

The problem is I have to know the text file encoding before it's read to NSString object.
0
 
LVL 33

Accepted Solution

by:
pgnatyuk earned 500 total points
ID: 36900451
For Mac OS X, probably, the simplest way is to try to load the data with different encoding types. In case of an error the error object you pass will be not nil.
Look for paragraph "Reading data with an unknown encoding" here:
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Strings/Articles/readingFiles.html
You will find the source code there too.

For iPhone you can use something like that:
NSString* thePath = [[NSBundle mainBundle] pathForResource:@"OneFile" ofType:@"txt"];
NSData* theData = [NSData dataWithContentsOfFile:thePath];
if (theData)
{
   // do something with the read data. For example, analyze and convert to NSString.
   NSString* something = [[NSString alloc] initWithData:theData encoding:NSUTF8StringEncoding];  
   if (something)
   {
       // fine. we could try NSASCIIStringEncoding too.
    }
}
0
 
LVL 4

Author Closing Comment

by:jacekmalinowski
ID: 36900667
Thanks a lot!
NSString *txtFile = [NSString stringWithContentsOfFile:filePath usedEncoding:encodingOfFile error:&error];

encodingOfFile returns encoding of the file.
0
 
LVL 33

Expert Comment

by:pgnatyuk
ID: 36901800
Cool. :)
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

What's a UDID? If you're involved in developing, testing, or even reviewing an iOS application that's in beta, then at some point you may need to know the UDID for any iOS devices that you'll be testing on. What's the UDID? It stands for Unique Dev…
In this article we discuss how to recover the missing Outlook 2011 for Mac data like Emails and Contacts manually.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now