uint64 Filesize

5 posts / 0 new
Last post
mritter0
mritter0's picture
Offline
Last seen: 2 years 5 months ago
Joined: 2014-04-21 21:15
uint64 Filesize

This is my first time using uint64 to handle file sizes >2GB. I am having a terrible time converting it to a readable value to show as text.

Code snippet I am using to convert 3534359552 to GB

  1. uint64 size, value;
  2.  
  3. // size=3534359552;
  4. value=(size/1073741824*100000)/100000; // = 3.291628...... rounded to 3
  5.  
  6. - or -
  7.  
  8. value=(size*100)/1073741824; // = 329.16288.... rounded to 329

How to I format it to be like: 3.29 GB

This crashes:

  1. IUtility->SNPrintf(BTUBuffer,15,"%llu%s%02llu GB",value,Locale->loc_DecimalPoint,value % 100);
salass00
salass00's picture
Offline
Last seen: 6 months 2 weeks ago
Joined: 2011-02-03 11:27
Re: uint64 Filesize
  1. /* Compile with:
  2.   gcc -O2 -Wall -Werror -Wwrite-strings -D__USE_INLINE__ -o uint64-filesize uint64-filesize.c -lauto
  3. */
  4. #include <proto/dos.h>
  5. #include <proto/utility.h>
  6. #include <proto/locale.h>
  7.  
  8. int main(void) {
  9. struct Locale *locale;
  10. char buffer[16];
  11. uint64 size = 3534359552ULL;
  12. uint64 value = (size*100 + (1024*1024*1024/2)) / (1024*1024*1024);
  13.  
  14. locale = OpenLocale(NULL);
  15.  
  16. SNPrintf(buffer, sizeof(buffer), "%llu%s%s%02llu GB", value / 100,
  17. locale ? locale->loc_DecimalPoint : ".", "", value % 100);
  18. Printf("Size: %s\n", buffer);
  19.  
  20. CloseLocale(locale);
  21.  
  22. return RETURN_OK;
  23. }

There is a bit of a problem with 64-bit values and RawDoFmt() based functions like SNPrintf() in that while RawDoFmt() doesn't expect any alignment the C compiler aligns them to 64-bit so that's why I've added an extra %s and "" to work around this.

salass00
salass00's picture
Offline
Last seen: 6 months 2 weeks ago
Joined: 2011-02-03 11:27
Re: uint64 Filesize

BTW since the fraction part is limited to 0-99 there is strictly no need to use a 64-bit value there so the SNPrintf() could be changed to:

  1. SNPrintf(buffer, sizeof(buffer), "%llu%s%02lu GB", value / 100,
  2. locale ? locale->loc_DecimalPoint : ".", (uint32)(value % 100));
mritter0
mritter0's picture
Offline
Last seen: 2 years 5 months ago
Joined: 2014-04-21 21:15
Re: uint64 Filesize

Thank you. Works just like I needed.

Are there any other 64bit issues I need to look out for?

salass00
salass00's picture
Offline
Last seen: 6 months 2 weeks ago
Joined: 2011-02-03 11:27
Re: uint64 Filesize


Are there any other 64bit issues I need to look out for?

Not that I can think of.

Log in or register to post comments