【问题标题】:Emacs hexl-mode UTF8 BOM issueEmacs hexl-mode UTF8 BOM 问题
【发布时间】:2011-09-08 12:08:39
【问题描述】:

我在 Emacs (GNU Emacs 22.2.1 / Debian GNU Linux) 下使用 hexl-mode 时遇到了一些奇怪的问题。

我有一个 UTF8 文本文件,我想向其附加 BOM(字节顺序掩码:即使不建议将无意义的 BOM 附加到 UTF8 文件,规范明确指定 UTF8 文件中的 BOM 是合法的)。

file 命令查看文件的方式如下:

...$  file  /tmp/test.txt
/tmp/test.txt: UTF-8 Unicode English text

以下作品:

open the UTF8 file (without BOM) in text mode
add three ASCII characters at the beginning of the file
close the file   (<-- see, very important, I need to close the file)
M-x hexl-mode
M-x hexl-find-file  (re-opening the file but this time in hexl-mode)
M-x hexl-insert-hex-string
EFBBBF
C-x C-s (saving the file)
M-x hexl-mode-exit

然后我得到一个带有 BOM 的 UTF-8 文件,如 file 命令所示:

...$  file  /tmp/test.txt
/tmp/test.txt: UTF-8 Unicode (with BOM) English text

(请注意,文件命令以启发式方式将其检测为带有 BOM“英文文本”的 UTF-8,但该文件确实包含很多欧元符号:我的意思是,在添加 BOM 之前,它不是一个 ASCII 文件但已经是一个 UTF-8 文件,如上所示)

但是我根本无法在 Emacs 下打开文件 first 然后调用 hexl-mode 然后尝试用 0xEB 0xFF 0xBF (BOM)替换前三个字符,然后保存。

显然,从 (Text) 切换到 (Hexl) 模式时会出现疯狂的转换问题。

我是否遗漏了一些明显的东西,或者与 Text / Hexl 之间的转换有点损坏,我最好先切换到 hexl-mode,进行十六进制编辑,然后保存并关闭文件并以文本模式重新打开?

【问题讨论】:

标签: emacs utf-8


【解决方案1】:

如果您查看hexl-find-file 代码,您会看到它调用find-file-literally,然后切换到hexl-mode

来自find-file-literally的文档

访问文件 FILENAME 而不进行任何类型的转换。格式转换 和字符代码转换都被禁用,并且多字节 结果缓冲区中的字符被禁用。

所以你可以用find-file-literally打开你的文件,添加3个字符,然后切换到hexl-mode

【讨论】:

    【解决方案2】:

    请注意,带有此标签的 xml 文件将在保存时以静默方式转换为 utf-16 大端。

    <?xml version="1.0" encoding="UTF-16"?>
    

    这将在更改并保存后自动将文件 utf8 与 bom:

    <?xml version="1.0" encoding="UTF-8"?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-24
      • 2017-07-08
      • 2011-09-23
      • 1970-01-01
      • 2017-11-12
      相关资源
      最近更新 更多