Renders human readable numbers

ss_bytes is a function defined in sslib.c.

Synopsis:

char * ss_bytes(hsize_t nbytes, char *buf)

Formal Arguments:

  • buf: Optional buffer to hold the results. If the user supplies the buffer then it should be large enough to hold the result. On a 64-bit machine that would be at least 62 bytes. If the caller passes the null pointer then one of six static buffers will be used (don’t make more than six calls to this function in a single printf argument list).

Description: Often when printing a very large decimal number it’s not obvious whether that number is some multiple of a power of 1024. This function breaks down the nbytes argument into GB, MB, and kB and stores it in a buffer supplied by the caller or a buffer allocated in this function. The output format will be something along the lines of:

1
 4,197,386 (4M+3k+10)

If the nbytes has all bits set then it will be printed in hexadecimal. If it is less than 1024 then the parenthesised part will be omitted.

This function has no provision for limiting the size of the result string. The maximum string length on a 64-bit machine would be 62 bytes counting the NUL terminator:

1
 ##,###,###,###,###,###,### (##,###,###,###G+####M+####k+####)

Return Value: Returns a pointer to the NUL-terminated string containing the number on success; null on failure.

Parallel Notes: Independent

See Also:

  • Library: Introduction for current chapter