Quantcast
Viewing latest article 8
Browse Latest Browse All 21

Answer by mikeserv for A standard tool to convert a byte-count into human KiB MiB etc; like du, ls1

The answer to your question is yes.

While the output format isn't exactly to your specification, the conversion itself is easily done by a very standard tool (or two). The ones to which I refer are dc and bc. You can get a segmented report by altering their output radices. Like this:

{   echo 1024 o           #set dc's output radix
    echo 1023 pc          #echo a number then print + clear commands
    echo 1024 pc
    echo 1025 pc
    echo 8000000 pc
} | dc

...which prints...

 1023                    #1 field 1023 bytes
 0001 0000               #2 fields 1k 0b
 0001 0001               #2 fields 1k 1b
 0007 0644 0512          #3 fields 7m 644k 512b or 7.64m

I use dc above because it is a personal favorite, but bc can do the same with different syntax and adheres to the same format rules as specified by POSIX like:

  • bc obase

    • For bases greater than 16, each digit shall be written as a separate multi-digit decimal number. Each digit except the most significant fractional digit shall be preceded by a single space. For bases from 17 to 100, bc shall write two-digit decimal numbers; for bases from 101 to 1000, three-digit decimal strings, and so on. For example, the decimal number 1024 in base 25 would be written as:

    01 15 24

    and in base 125, as:

    008 024


Viewing latest article 8
Browse Latest Browse All 21

Trending Articles