Solved

How do i detect the encoding of a text file?

Posted on 2011-10-01
5
1,590 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

CocoaPods is the best way to manage library dependencies in iOS and OS X projects. By using cocoa pods there is no need of downloading the code from github and copying to your project. There are plenty of open source libraries now available with C…
I recently went through the process of creating an installable image of an iPhone mobile app (an .ipa file) that beta testers could load onto their test devices. The process involved quite a few steps, but both Appcelerator and Diawi.org had decent …
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
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.

813 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

11 Experts available now in Live!

Get 1:1 Help Now