I’ve never been particularly good at math. Sometimes, the numbers get challenging to keep straight, especially when moving in orders of magnitude from one value to another. But, when working with large numbers, I typically codify conversion code to help ease the mental burden of keeping everything straight. I know I need to act, especially once scientific notation shows in my output results.
So, in this post, I’ve written a helper class that helps convert from bytes to kilobytes, megabytes, gigabytes, and terabytes.
How To Convert Bytes To Something Else
For folks just looking for a mathematical formula to convert bytes, it’s pretty straightforward. Every permutation above bytes increases by 1024 ^ N, where N is an integer value.
Technically, the value of
1024
is used for kibibytes, mebibytes, gibibytes and tebibytes.
Use the appropriate value of 1000
or 1024
based on your accuracy needs, but either should give you a ballpark idea of sizes you’ll be dealing with at each level of differentiation.
Here is an enumeration showing the increase in N
. Of course, you could continue to add more if you need petabytes
or exabyte
, but that starts to get into excessive digit representations.
To change a higher-order classification (i.e., megabytes) to bytes, you need to multiply value * 1024^N
where N
in this case is 2
. To reverse the process, you need to divide. In our case, going from bytes to megabytes would be value / 1024 ^ 2
. It looks like this in code.
You could stop here, and you have a perfectly efficient way of doing conversions. Still, you could add helper methods to make things easier to read in your existing codebase.
Here’s a unit test class that helps verify everything is working.
Use Humanizer Instead
Humanizer is a library designed to help you manipulate strings, enums, dates, times, timespans, numbers, and quantities. Within the library is a type called ByteSize
which has nice helper functions to convert between size groups. The nice thing about having a type is the core value never changes, and you can quickly translate between all delineations.
In addition (no pun intended), you can do math between multiple sizes.
It’s definitely worth checking out if you think you’ll be needing both conversions and ways to display your results between all the variations. To get started, install the latest version using the dotnet-cli
.
How About Using UnitsNet
If you need more options, I also recently found out about UnitsNet, which looks like a great option for all different units of measure. Here’s a sneak peek of what the library can do with Bytes.
The resulting output in the console is what you would expect.
1000kb
1mb
0.001gb
The other advantage to using UnitsNet is it comes with a library of other units and measurements, allowing you to do some very complex calculations while still keeping your code readable. It’s pretty awesome.
I hope this helps folks that may be doing conversions in their code. Thanks for reading, and I wish you the best of luck in your development journey. Cheers!