https://github.com/gsemac/byte-size
A class for easily displaying byte quantities in different formats
https://github.com/gsemac/byte-size
Last synced: 12 months ago
JSON representation
A class for easily displaying byte quantities in different formats
- Host: GitHub
- URL: https://github.com/gsemac/byte-size
- Owner: gsemac
- License: mit
- Created: 2017-06-29T01:34:51.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2017-08-23T08:13:37.000Z (almost 9 years ago)
- Last Synced: 2025-06-05T07:02:01.045Z (about 1 year ago)
- Language: C++
- Homepage:
- Size: 39.1 KB
- Stars: 1
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# byte-size
A class for easily displaying byte quantities in different formats
`ByteSize` (and the accompanying class, `BitSize`) was inspired by Omar's [ByteSize](https://github.com/omar/ByteSize) class for C#.
Use the `ByteSize` class to represent amounts in bytes, kilobytes, megabytes, gigabytes, and so on (up to petabytes):
```cpp
ByteSize bs(1024);
std::cout << bs.Bits(); // outputs 8192
std::cout << bs.Bytes(); // outputs 1024
std::cout << bs.Kilobytes(); // outputs 1
std::cout << bs; // outputs 1.00 KiB
```
By default, the binary prefix is used (`1 kilobyte` = `1024 bytes`). The decimal prefix can be used by providing it in the constructor:
```cpp
ByteSize bs(1024, BytePrefix::Decimal); // 1 kilobyte = 1000 bytes
std::cout << bs; // outputs 1.02 kB
```
The `BitSize` class works similarly, but is used to represent amounts in kilobits, megabits, gigabits, and so on (up to petabits):
```cpp
BitSize bs_binary(1024);
BitSize bs_decimal(1024, BytePrefix::Decimal);
std::cout << bs_binary; // outputs 8.00 Kibit
std::cout << bs_decimal; // outputs 8.19 kbit
```
You can also create an instance of either class from a `string`, and the correct prefix will be deduced:
```cpp
ByteSize bs = ByteSize::Parse("1kB");
std::cout << bs.ToString(1); // outputs 1.0 kB
```
Various methods exist to create an instance of either class from a given unit:
```cpp
ByteSize bs = ByteSize::FromMegabytes(0.5);
std::cout << bs; // outputs 512 KiB
```
#### License
Released under [MIT License](https://github.com/gsemac/byte-size/blob/master/LICENSE).