我试图在我的系统中获取MD5哈希文件及其返回的无效字符。 [英] I am trying to get MD5 hash of files in my system and its returning invalid characters.

查看:73
本文介绍了我试图在我的系统中获取MD5哈希文件及其返回的无效字符。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我系统上的文件计算MD5哈希值。



I am trying to calculate MD5 hash for files on my system.

 String md5 = CreateMD5(file); //file is the String containing the filename.
labelNew.Text = md5;




public static String CreateMD5(string filename)
{
 using (var md5 = MD5.Create())
 {
  using (var stream = File.OpenRead(filename))
  {
   return BitConverter.ToString(md5.ComputeHash(stream)).Replace("-", "‌​").ToLower();
  }
 }
}





输出



Output is

d8??eb??f6??e3??b5??1c??33??d5??2d??bd??3d??3a??47??3f??2b??3c

相反

d8ebf6e3b51c33d52dbd3d3a473f2b3c





我尝试过:



奇怪的是,这些字符只出现在Console.WriteLine()中,而不是出现在窗体中。





What I have tried:

Its strange that the characters appear in the Console.WriteLine() only and not in the windows form.

Console.WriteLine("New {0}\nOld {1}",labelNew.Text.Replace("?",""), labelExist.Text);

推荐答案


kuharan[^]:

奇怪的是,这些字符只出现在Console.WriteLine()中,而不是出现在windows窗体中。

Its strange that the characters appear in the Console.WriteLine() only and not in the windows form.



确实很奇怪,我也好奇并做了一些谷歌搜索并进入这篇文章,描述相同的场景,并将其归咎于编码不匹配。



c# - 问号(?)附加到字符串 - 堆栈溢出 [ ^ ]

线程建议使用正则表达式删除非ASCII字符


Strange indeed, I was curious too and did some Googling and run into this post, describe the same scenario and blame it on "encoding mismatch".

c# - Question Mark ("?") Getting Appended to String - Stack Overflow[^]
The thread suggest to use regular expression to remove the Non-ASCII characters

Console.WriteLine("New {0}\nOld {1}", Regex.Replace(labelNew.Text, @"[^\u0000-\u007F]", string.Empty), labelExist.Text);



实际上,我认为这不是问题吗?因为代码将结果输出到标签labelNew.Text = md5;?


In reality, I don't think this is an issue right? Because the code is outputting the result into a label "labelNew.Text = md5;"?


它适用于我:

It works for me:
private void button1_Click(object sender, EventArgs e)
    {
    string s = CreateMD5(@"D:\CP data.txt");
    }
public static String CreateMD5(string filename)
    {
    using (var md5 = MD5.Create())
        {
        using (var stream = File.OpenRead(filename))
            {
            return BitConverter.ToString(md5.ComputeHash(stream)).Replace("-", "‌​").ToLower();
            }
        }
    }

在调试器中查看 s 给了我:

Looking at s in the debugger gives me:

s   "6d‌​f2‌​50‌​15‌​ce‌​ff‌​e1‌​59‌​6e‌​14‌​84‌​c0‌​34‌​f6‌​62‌​39"    string

这是我所期待的。

那么我在做什么与你有什么不同呢?

Which is what I'd expect.
So what am I doing that is different from you?


这篇关于我试图在我的系统中获取MD5哈希文件及其返回的无效字符。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆