Ioana
Ioana

Reputation: 53

Conversion from GB to DWORD or vice-versa

I want to compare a filesize to check if it is below 8GB. How could I convert either the filseSize( which is in DWORD) to GB or the 8 GB to DWORD?

Thank you!

Upvotes: -3

Views: 195

Answers (1)

selbie
selbie

Reputation: 104474

I'm guessing from your DWORD hint, you are on the Windows platform and using Win32 APIs to get file sizes.

Don't call GetFileSize. It's limited to a DWORD (32-bit), which I think it what your question is about.

Instead, invoke GetFileSizeEx, which gives you back a 64-bit result for a file handle. Or GetFileAttributesEx which gives back a struct with a 64-bit size in it split across two dwords.

Example:

const LONGLONG MAX_FILE_SIZE = 1024LL * 1024LL * 1024LL * 8;
LARGE_INTEGER li = {0};
if (GetFileSizeEx(hFile, &li)) {
    LONGLONG filesize = li.QuadPart;
    if (filesize > MAX_FILE_SIZE) {
        ...
    }
}

OR

const LONGLONG MAX_FILE_SIZE = 1024LL * 1024LL * 1024LL * 8;
WIN32_FILE_ATTRIBUTE_DATA info = {0};
if (GetFileAttributesEx(filename, GetFileExInfoStandard, (void*)&info)) {
    LONGLONG filesize = info.nFileSizeHigh;
    filesize  = filesize << 32;
    filesize |= info.nFileSizeLow;
    if (filesize > MAX_FILE_SIZE) {
        ...
    }
 }

Upvotes: 2

Related Questions