brew install enca
enca filepath.txt
示例
$ enca 动态规划算法.txt [0]
Simplified Chinese National Standard; GB2312
CRLF line terminators
在macOS 上打开一些 .txt
文件,会显示乱码(非 utf-8 编码),我想使用 iconv 命令对文件进行转码。
此时使用 file
命令来获得文件编码,在使用 iconv 转码时会出错。使用 enca 获取的则正确。
副 iconv 转码命令:
iconv -f GB2312 -t UTF-8 分治算法.txt > 分治算法2.txt
iconv 使用可详见:https://ezcode.blog.csdn.net/article/details/146444439
如果解决问题之余,还想更多了解 enca 可以往下看。
enca github : https://github.com/nijel/enca
Enca : Extremely Naive Charset Analyser,极简的字符集分析器
由两个主要组件组成:
Enca 应该可以在每个符合 POSIX.1 规范的系统上使用 ISO C 编译器编译并运行,实际上它也可以在许多不符合规范的系统上编译(下面列出依赖项)。
如果您拥有以下附加工具,Enca 可以使用它们作为外部转换器:
可选功能:
--with-librecode[=DIR]
,--without-librecode
配置参数控制。DIR/include
中搜索,库本身在 DIR/lib
中。--with-libiconv=[DIR]
,--without-libiconv
配置参数控制。--enable-external
,--disable-external
配置参数控制。默认情况下是编译进去的。不要尝试在以下不支持 ISO C 和 POSIX 功能的系统上编译 Enca:
对于有耐心的人:像往常一样运行以下命令
./configure
make
make check
make install
输入 enca --help
可以查看可用选项
也可以输入 man enca
获得更多说明
$ enca --help [0]
Usage: enca [-L LANGUAGE] [OPTION]... [FILE]...
enconv [-L LANGUAGE] [OPTION]... [FILE]...
Detect encoding of text files and convert them if required.
Operation modes:
-g, --guess Behave as `enca' (just detect encoding by default)
-c, --auto-convert Behave as `enconv' (autoconvert by default)
Output type selectors:
-d, --details Print failure reason when encoding was not recognized
-e, --enca-name Print enca's encoding name (passed to converters)
-f, --human-readable Print full (descriptive) encoding name (default)
-i, --iconv-name Print how iconv calls the encoding
-m, --mime-name Print preferred MIME encoding name
-r, --rfc1345-name Print RFC 1345 (or otherwise canonized) encoding name
-s, --cstocs-name Print how cstocs calls the encoding
-n, --name=WORD Print required name (enca-name, human-readable, etc.)
-x, --convert-to=ENC Convert file to some other encoding ENC
Guessing parameters:
-L, --language=LANG Set language of FILEs; obligatory, when cannot be
determined from locale settings
Conversion parameters:
-E, --external-converter-program=PATH
Set external converter program name
(default: piconv)
-C, --try-converters=LIST Converters to be tried (associative)
(default: built-in,iconv)
General options:
-p, --with-filename Print the file name for each result
-P, --no-filename Suppress the prefixing filename on output
-V, --verbose Increase verbosity level
Listings:
-G, --license Print full enca license and terminate
-h, --help Print this help and terminate
-l, --list=WORD Print required list (built-in-charsets, converters,
charsets, languages, lists, names, surfaces)
and terminate
-v, --version Print version and build information and terminate
With no FILE, reads standard input and possibly writes converted stream to
standard output. Exit status is 0 if all files were successfully proceeded,
1 if some were not recognized or converted, 2 in real troubles.
When called as `enconv' without -x, target encoding it guessed from locales.
Report bugs to https://github.com/nijel/enca/issues
2025-03-22(六)