Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个包含 UTF-8 字符序列的纯文本文件 (.yml),如下所示:
foo: "检测到 \xC3\xB8"
问题在于 \xC3\xB8 - 这些不是“真正的”UTF-8 字节,因为它们在文本文件中保存为 8 个实际字符:\ x C 3 \ x B 8
有没有办法将这些转换成真正的 2 字节 UTF-8 序列?
可以使用任何操作系统/语言/外壳工具:-)
/ 卡斯滕
使用这个 perl 脚本来转换你的文件:
#!/usr/bin/perl while (<STDIN>) { $_ =~ s/\\x([0-9A-F][0-9A-F])/chr(hex($1))/eg; print $_; }
假设您将一个文件命名为 script bogusutf,然后使用以下命令进行转换:
bogusutf
$ perl bogusutf <输入文件>输出文件