Solved

How do i detect the encoding of a text file?

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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

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…
In this article we will discuss some EI Capitan Mail app issues and provide some manual process to resolve them.
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.

739 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