将未知编码的TXT文件转换为字符串 [英] Convert TXT File of Unknown Encoding to String

查看:206
本文介绍了将未知编码的TXT文件转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果编码类型未知,我如何将纯文本(.txt)文件转换为字符串?

How can I convert Plain Text (.txt) files to a string if the encoding type is unknown?

我正在开发一个允许用户将txt文件导入我的应用程序。这意味着文件可能已经在任何数量的应用程序中创建,使用任何可被认为对纯文本文件有效的编码。我的理解是可以包括(ASCII,UTF-8,UTF-16,UTF-16BE,UTF-16LE,UTF-32,UTF-32BE,UTF-32LE或EBCDIC?!)

I'm working on a feature that would allow users to import txt files into my app. This means the file could have been created in any number of apps, utilizing any of a variety of encodings that would be considered valid for a plain text file. My understanding is this could include (ASCII, UTF-8, UTF-16, UTF-16BE, UTF-16LE, UTF-32, UTF-32BE, UTF-32LE, or EBCDIC?!)

使用以下内容,事情进展顺利:

Things had been going well using the following:

NSString *txtFileAsString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&errorReading];

然后,用户提供导入时导致空的内容的文件。我在XCode调试中看过该文件,看到Cocoa错误261,NSStringEncoding = 4。

Then a user supplied a file that resulted in empty content when imported. I watched the file in XCode debug, and see a Cocoa error 261, NSStringEncoding=4.

我知道:


  • 用户提供的文件是使用名为knowtes的应用程序创建的。

  • 该文件在Mac OS X上使用TextEdit,TextWranger等打开

  • 该文件包含特殊字符,如umlauts(rant:为什么umlaut上的u不具有umlaut?!)

  • Finder信息显示:

  • The user supplied file was created with an app called knowtes
  • The file opens with TextEdit, TextWranger, etc. on Mac OS X
  • The file contains "special characters" such as umlauts (rant: why doesn't the "u" on umlaut have an umlaut?!)
  • Finder Info displays:

种类:文本

Kind: text



查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆