gpt4 book ai didi

perl - hexdigest在linux和windows中为同一文件生成不同的值

转载 作者:行者123 更新时间:2023-12-02 21:32:14 26 4
gpt4 key购买 nike


我正在使用 Digest::MD5 模块,并且该 hexdigest 为 Windows 和 Linux 返回不同的值。

请帮我解决这个问题。

use Digest::MD5;
my $ctx=Digest::MD5->new();
open RD, "input.txt";
$ctx->addfile(*RD);
close RD;
print $ctx->hexdigest;

input.txt 文件包含以下内容:

hello

输出:窗口

5d41402abc4b2a76b9719d911017c592

输出:Linux

af5597c29467a96523a70787c319f4db

谢谢,
沙拉瓦南

最佳答案

根据提问者给出的评论,input.txt是在Windows中生成的,然后复制到Linux中,那么我认为问题是由Windows和Linux端的差异引起的- of-line,在 Windows 中行尾是 '\r\n',在 Linux 中是'\n'。因此,使用 binmode()将文件处理程序设置为二进制模式应该能够解决此问题,如 Digest::MD5 示例中所建议的那样.

这应该可以解决问题:

#!/usr/bin/perl

use warnings;
use strict;

use Digest::MD5;

open my $fh, '<', 'input.txt' or die "Cannot open input.txt: $!";
binmode $fh, ':raw'; # <==== NOTE this binmode()

my $md5 = Digest::MD5->new;
$md5->addfile($fh);
print $md5->hexdigest, "\n";
close $fh;

关于perl - hexdigest在linux和windows中为同一文件生成不同的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22092534/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com