【问题标题】:Get file size as hex in C#在 C# 中获取文件大小为十六进制
【发布时间】:2013-11-05 11:37:13
【问题描述】:

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

到目前为止我的代码:

     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:\Users\Admin\Desktop\10 23\files");
        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:\Users\Admin\Desktop\10 23\files");
        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);
}

【问题讨论】:

    标签: c# c++ hex filesize


    【解决方案1】:

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

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

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

    【讨论】:

    • 当然我必须等 10 分钟。问题解决了!
    猜你喜欢
    • 1970-01-01
    • 2012-04-04
    • 2015-02-14
    • 2014-12-24
    • 2014-03-30
    • 2015-05-01
    • 2012-03-26
    • 2015-01-26
    • 1970-01-01
    相关资源
    最近更新 更多