About
Convert byte length strings such as "5kb" to a long long and vice versa.
Installation
Copy byte.h and byte.c to your source code tree.
Usage
void test_string_to_bytes(void) {
assert(100 == string_to_bytes("100"));
assert(100 == string_to_bytes("100b"));
assert(100 == string_to_bytes("100 bytes"));
assert(1024 == string_to_bytes("1kb"));
assert(1024 * 1024 == string_to_bytes("1mb"));
assert(1024 * 1024 * 1024 == string_to_bytes("1gb"));
assert(2 * 1024 == string_to_bytes("2kb"));
assert(5 * 1024 * 1024 == string_to_bytes("5mb"));
}
void test_bytes_to_string(void) {
equal("100b", bytes_to_string(100));
equal("1kb", bytes_to_string(1024));
equal("1mb", bytes_to_string(1024 * 1024));
equal("5mb", bytes_to_string(1024 * 1024 * 5));
equal("1gb", bytes_to_string(1024 * 1024 * 1024));
equal("5gb", bytes_to_string((long long) 1024 * 1024 * 1024 * 5));
}
API
long long
string_to_bytes(const char *str);
char *
bytes_to_string(long long bytes);
License
MIT
|