{"id":114,"date":"2013-04-16T15:49:59","date_gmt":"2013-04-16T19:49:59","guid":{"rendered":"http:\/\/www.mbeckler.org\/blog\/?p=114"},"modified":"2013-04-16T20:35:36","modified_gmt":"2013-04-17T00:35:36","slug":"pretty-printing-a-number-of-bytes-in-cc","status":"publish","type":"post","link":"https:\/\/www.mbeckler.org\/blog\/?p=114","title":{"rendered":"Pretty printing a number of bytes in C\/C++"},"content":{"rendered":"<p>I spend a lot of time write and debugging command line scripts and programs. As much as I like looking at large numbers (millions, billions, trillions, etc) it can be difficult to read a big number and quickly parse how large it is, i.e. &#8220;is that 12 megabytes or 1.2 gigabytes?&#8221;.<\/p>\n<p>A long time ago I wrote a small function that does pretty printing of a number of bytes. It can handle from bytes to exabytes, and properly handles integer numbers of a unit by printing it as an integer instead of float. Should be easy enough to adjust to your specific needs or style desires.<\/p>\n<pre lang=\"c\">\/\/ Prints to the provided buffer a nice number of bytes (KB, MB, GB, etc)\r\nvoid pretty_bytes(char* buf, uint bytes)\r\n{\r\n    const char* suffixes[7];\r\n    suffixes[0] = \"B\";\r\n    suffixes[1] = \"KB\";\r\n    suffixes[2] = \"MB\";\r\n    suffixes[3] = \"GB\";\r\n    suffixes[4] = \"TB\";\r\n    suffixes[5] = \"PB\";\r\n    suffixes[6] = \"EB\";\r\n    uint s = 0; \/\/ which suffix to use\r\n    double count = bytes;\r\n    while (count >= 1024 && s < 7)\r\n    {\r\n        s++;\r\n        count \/= 1024;\r\n    }\r\n    if (count - floor(count) == 0.0)\r\n        sprintf(buf, \"%d %s\", (int)count, suffixes[s]);\r\n    else\r\n        sprintf(buf, \"%.1f %s\", count, suffixes[s]);\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I spend a lot of time write and debugging command line scripts and programs. As much as I like looking at large numbers (millions, billions, trillions, etc) it can be difficult to read a big number and quickly parse how large it is, i.e. &#8220;is that 12 megabytes or 1.2 gigabytes?&#8221;. A long time ago [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[1],"tags":[37,38,20],"class_list":["post-114","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-c","tag-cpp","tag-programming"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p2BznB-1Q","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.mbeckler.org\/blog\/index.php?rest_route=\/wp\/v2\/posts\/114","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mbeckler.org\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mbeckler.org\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mbeckler.org\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mbeckler.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=114"}],"version-history":[{"count":4,"href":"https:\/\/www.mbeckler.org\/blog\/index.php?rest_route=\/wp\/v2\/posts\/114\/revisions"}],"predecessor-version":[{"id":118,"href":"https:\/\/www.mbeckler.org\/blog\/index.php?rest_route=\/wp\/v2\/posts\/114\/revisions\/118"}],"wp:attachment":[{"href":"https:\/\/www.mbeckler.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=114"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mbeckler.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=114"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mbeckler.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=114"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}