Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Shell 如何在perl中将ascii编码文件转换为utf-8编码?_Shell_File_Perl_Utf 8_Ascii - Fatal编程技术网

Shell 如何在perl中将ascii编码文件转换为utf-8编码?

Shell 如何在perl中将ascii编码文件转换为utf-8编码?,shell,file,perl,utf-8,ascii,Shell,File,Perl,Utf 8,Ascii,我想将ascii编码的文本文件转换为utf-8编码。 到目前为止,我已经尝试过: open( my $test, ">:encoding(utf-8)", $test_file ) or die("Error: Could not open file!\n"); 并运行下面的命令,该命令显示文件的编码 file $test_file test_file: ASCII text 如果我在这里遗漏了什么,请告诉我。任何ASCII格式的文件(即仅包含0

我想将ascii编码的文本文件转换为utf-8编码。 到目前为止,我已经尝试过:

open( my $test, ">:encoding(utf-8)", $test_file ) or die("Error: Could not open file!\n");
并运行下面的命令,该命令显示文件的编码

file $test_file
test_file: ASCII text
如果我在这里遗漏了什么,请告诉我。

任何ASCII格式的文件(即仅包含0到127的代码点)都已在UTF-8中。在编码上没有区别,因此,
文件
无法将其识别为UTF-8

编码上的差异仅发生在代码点为128的字符上

它是为了向后兼容ASCII而设计的:Unicode的前128个字符(与ASCII一一对应)使用与ASCII具有相同二进制值的单个字节进行编码,因此有效的ASCII文本也是有效的UTF-8编码Unicode

(摘自)


你做得对

ASCII是UTF-8的子集

解码编码
ASCII码⇒   统一码⇒   UTF-8
----------      ----------      ----------
00 U+0000 00
01 U+0001 01
02 U+0002 02
⋮               ⋮               ⋮
7E U+007E 7E
7F U+007F 7F
----------      ----------      ----------
ASCII码⇐   统一码⇐   UTF-8
编解码
因此,ASCII文件是UTF-8文件。[1]

仅使用该子集时,
file
将文件标识为使用ASCII编码

$perl-M5.010-e'使用utf8;使用open“:std”和“:encoding(UTF-8)”;说“abcdef”|文件-
/dev/stdin:ASCII文本
超出该子集将导致
文件
将该文件标识为使用UTF-8编码的文本

$perl-M5.010-e'使用utf8;使用open“:std”和“:encoding(UTF-8)”;说“abcdéf”|文件-
/dev/stdin:UTF-8unicode文本

  • 它也是iso-latin-1文件、iso-latin-2文件、iso-latin-3文件、cp1250文件、cp1251文件、cp1252文件等

  • 你在测试文件中写了什么吗?
    file
    命令通过查看文件的内容来确定文件类型file@HåkonHægland是的,我在打开文件后添加了一些内容。如何更改内容的编码。请显示您添加的内容的示例。例如,如果只添加ASCII字符,则UTF-8和ASCII之间没有差异,
    文件
    无法确定差异您正在正确执行。ASCII是UTF-8的子集,因此如果仅使用该子集,
    文件
    将报告ASCII。