Wolfram Research

Function Repository Resource:

BytesToQuantity

Source Notebook

Convert a byte count to a quantity of bytes in an appropriate unit

Contributed by: Richard Hennigan (Wolfram Research)

ResourceFunction["BytesToQuantity"][int]

converts an integer int representing a byte count to a Quantity using a compatible unit.

ResourceFunction["BytesToQuantity"][Quantity[]]

converts a quantity of bytes to an appropriate compatible unit.

Examples

Basic Examples

Convert an integer of bytes to a Quantity:

In[1]:=
ResourceFunction["BytesToQuantity"][123]
Out[1]=

Convert a lot of bytes into yottabytes:

In[2]:=
ResourceFunction["BytesToQuantity"][25!]
Out[2]=

Convert a compatible Quantity:

In[3]:=
ResourceFunction["BytesToQuantity"][Quantity[12345, "Petabytes"]]
Out[3]=

Applications

Get a prettier output for ByteCount:

In[4]:=
ResourceFunction["BytesToQuantity"]@ByteCount@Range[123456789]
Out[4]=

Properties and Relations

BytesToQuantity will choose units similar to the formatted display of ByteArray:

In[5]:=
ByteArray[RandomInteger[{0, 255}, 12345]]
Out[5]=
In[6]:=
ResourceFunction["BytesToQuantity"][Length[%]]
Out[6]=

Possible Issues

Units must be compatible:

In[7]:=
ResourceFunction["BytesToQuantity"][Quantity[500, "Miles"]]
Out[7]=

Requirements

Wolfram Language 11.3 (March 2018) or above

Resource History

License Information