Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How do i detect the encoding of a text file?

Posted on 2011-10-01
5
Medium Priority
?
1,815 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

The release of the iPhone 6 and the iPhone 6 Plus was extremely successful for Apple. However when iOS 8.1 was introduced, many iPhone users complained of quick battery drainage. Without even knowing, you may be unnecessarily using up your battery p…
In this article we have discussed about the OS X EI Capitan and how to fix Wi-Fi issue in OS X El Capitan. We have explained how to delete system level preferences and create a new Wi-Fi location to resolve Wi-Fi issue.
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.

722 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