文章目錄
- macOS 上安裝 enca
- 基本使用
- 起因 - iconv
- 關于 enca
- 安裝 Enca
- enca & enconv 其它用法
macOS 上安裝 enca
brew install enca
基本使用
enca filepath.txt
示例
$ enca 動態規劃算法.txt [0]
Simplified Chinese National Standard; GB2312CRLF line terminators
起因 - iconv
在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 可以往下看。
enca github : https://github.com/nijel/enca
Enca : Extremely Naive Charset Analyser,極簡的字符集分析器
由兩個主要組件組成:
- libenca,一個編碼檢測庫。
它目前支持白俄羅斯語、保加利亞語、克羅地亞語、捷克語、愛沙尼亞語、匈牙利語、拉脫維亞語、立陶宛語、波蘭語、俄語、斯洛伐克語、斯洛文尼亞語、烏克蘭語、中文以及一些獨立于語言的多字節編碼。
API 應該相對穩定(可以理解為“它要么只發生微小的變化,要么發生非常劇烈的變化”)。 - enca,一個命令行前端,集成了 libenca 和幾個字符集轉換庫和工具(GNU recode、UNIX98 iconv、perl Unicode::Map、cstocs)。
安裝 Enca
Enca 應該可以在每個符合 POSIX.1 規范的系統上使用 ISO C 編譯器編譯并運行,實際上它也可以在許多不符合規范的系統上編譯(下面列出依賴項)。
如果您擁有以下附加工具,Enca 可以使用它們作為外部轉換器:
- GNU recode 和相關的編碼庫
- Perl 字符集轉換器 Unicode::Map8 或 Unicode::Map
- cstocs,著名的捷克字符集轉換器
可選功能:
- GNU recode 庫接口的編譯由
--with-librecode[=DIR]
,--without-librecode
配置參數控制。
默認情況下,如果找到它則會編譯進去。您還可以指定一個文件夾;librecode 包含文件將在此DIR/include
中搜索,庫本身在DIR/lib
中。 - UNIX98 iconv 接口的編譯由
--with-libiconv=[DIR]
,--without-libiconv
配置參數控制。
默認情況下,如果找到并且被認為可用則會編譯進去。您還可以指定一個 DIR;libiconv 包含文件將在此 DIR/include 中搜索,庫本身在 DIR/lib 中。 - 外部轉換程序接口的編譯由
--enable-external
,--disable-external
配置參數控制。默認情況下是編譯進去的。
不要嘗試在以下不支持 ISO C 和 POSIX 功能的系統上編譯 Enca:
- 函數原型。
- 基本的 ISO C 頭文件及其聲明的函數:
- assert.h, ctype.h, math.h, stdarg.h, stdio.h, stdlib.h
- 任意(工作狀態)的 string.h, strings.h, memory.h
- unistd.h, sys/stat.h, sys/types.h
對于有耐心的人:像往常一樣運行以下命令
./configure
make
make check
make install
enca & enconv 其它用法
輸入 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 ENCGuessing parameters:-L, --language=LANG Set language of FILEs; obligatory, when cannot bedetermined from locale settingsConversion parameters:-E, --external-converter-program=PATHSet 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 levelListings:-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 terminateWith 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(六)