给出2 32位整数iMSB和iLSB
int iMSB = 12345678; // Most Significant Bits of file size in Bytes
int iLSB = 87654321; // Least Significant Bits of file size in Bytes
Run Code Online (Sandbox Code Playgroud)
漫长的形式将是......
// Always positive so use 31 bts
long long full_size = ((long long)iMSB << 31);
full_size += (long long)(iLSB);
Run Code Online (Sandbox Code Playgroud)
现在..
我不需要那么多的精度(确切的字节数),所以,我怎样才能将文件大小转换为MiBytes到3个小数位并转换为字符串...
试过这个......
long double file_size_megs = file_size_bytes / (1024 * 1024);
char strNumber[20];
sprintf(strNumber, "%ld", file_size_megs);
Run Code Online (Sandbox Code Playgroud)
......但似乎没有用.
即1234567899878Bytes = 1177375.698MiB ??
你误解了操作是如何工作的.你的计算应该是:
// Always use 32 bits
long long full_size = ((long long)iMSB << 32);
full_size += (unsigned long long)(iLSB);
Run Code Online (Sandbox Code Playgroud)
但是,12345678,87654321的组合不是 1234567887654321; 这是53024283344601009.
然后当你这样做
long double file_size_megs = file_size_bytes / (1024 * 1024);
char strNumber[20];
sprintf(strNumber, "%ld", file_size_megs);
Run Code Online (Sandbox Code Playgroud)
您正在使用long double(这是一种浮点格式)并%ld使用整数格式打印它.你的意思是:
long long file_size_megs = file_size_bytes / (1024 * 1024);
char strNumber[20];
sprintf(strNumber, "%lld", file_size_megs);
Run Code Online (Sandbox Code Playgroud)
另一种方法是仅计算MB中的文件大小:
long long file_size_megs = ((long long)iMSB << (32 - 20)) + ((unsigned)iLSB >> 20);
Run Code Online (Sandbox Code Playgroud)