Solved

How do i detect the encoding of a text file?

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
apache vhosts on mac not being recognized 5 71
Need to open a hard disk on a Mac 16 121
Mac Online Backups 7 59
Sluggish MBP browser 17 56
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 …
When Apple released Swift last year, the aim was to introduce a new programming language for Cocoa and Cocoa Touch that was fast, easy and effective, like the name connotes. Apple succeeded. Swift is designed to couple with Objective-C program…
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.

863 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

23 Experts available now in Live!

Get 1:1 Help Now