Frage

I am trying to convert filesizes from files to hex code but it gives me an error .

The code I have so far :

     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));
        }
    }     

The error is ".. cannot convert from 'long' to 'int' . Maybe someone know a better way to display the file size as hex .

I had this code in 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);
}
War es hilfreich?

Lösung

Why not change the DecToHex method to accept long instead.

FileInfo.Length returns a long and you can use a long as the parameter for string.Format.

public static string DecToHex(long decValue)
    {
        return string.Format("{0:x}", decValue);
    }
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top