在 C# 中获取十六进制文件大小

Get file size as hex in C#

本文关键字:十六进制 文件大小 获取      更新时间:2023-10-16

我正在尝试将文件大小从文件转换为十六进制代码,但它给了我一个错误.

到目前为止我拥有的代码:

     public static string DecToHex(int decValue)
    {
        return string.Format("{0:x}", decValue);
    }
    private void button1_Click(object sender, EventArgs e)
    {
        DirectoryInfo dinfo = new DirectoryInfo(@"C:UsersAdminDesktop10 23files");
        FileInfo[] Files = dinfo.GetFiles("*.xml");
        foreach (FileInfo file in Files)
        {
            listBox1.Items.Add(file.Name);
        }
    }
    private void button2_Click(object sender, EventArgs e)
    {
        DirectoryInfo dinfo = new DirectoryInfo(@"C:UsersAdminDesktop10 23files");
        FileInfo[] Files = dinfo.GetFiles("*.xml");
        foreach (FileInfo file in Files)
        {
            listBox2.Items.Add(DecToHex(file.Length));
        }
    }     

错误是"..无法从"long"转换为"int"。也许有人知道一种更好的方法 将文件大小显示为十六进制 .

我在 c++ 中有这段代码

if(m_bAlgorithm[HASHID_SIZE_32])
{
    sizehash32_end(&m_uSizeHash32);
    printf(SZ_SIZEHASH_32);
    printf(SZ_HASHPRE);
    printf("%08X", m_uSizeHash32);
    printf(CPS_NEWLINE);
}

为什么不将DecToHex方法更改为接受 long。

FileInfo.Length返回一个长整型,您可以使用长整型作为string.Format的参数。

public static string DecToHex(long decValue)
    {
        return string.Format("{0:x}", decValue);
    }