Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Convert a list of bits to a byte array
| ResourceFunction["BitListToByteArray"][bits] converts the list bits to a ByteArray. | 
Create a list of bits:
| In[1]:= | ![bits = RandomInteger[1, 160]](https://www.wolframcloud.com/obj/resourcesystem/images/148/1487d814-ec15-4616-938f-3919647b505a/0adcc82303590dea.png) | 
| Out[1]= |  | 
Convert to a byte array:
| In[2]:= | ![ba = ResourceFunction["BitListToByteArray"][bits]](https://www.wolframcloud.com/obj/resourcesystem/images/148/1487d814-ec15-4616-938f-3919647b505a/508208a537c414ee.png) | 
| Out[2]= |  | 
Compare the number of bytes needed to represent the expressions:
| In[3]:= |  | 
| Out[3]= |  | 
For larger bit arrays the relative memory compression does approach a factor of 64:
| In[4]:= | ![bits = RandomInteger[1, 16000];
ba = ResourceFunction["BitListToByteArray"][bits];
Divide @@ N[ByteCount /@ {bits, ba}]](https://www.wolframcloud.com/obj/resourcesystem/images/148/1487d814-ec15-4616-938f-3919647b505a/1e9c64275f6ad51a.png) | 
| Out[6]= |  | 
This work is licensed under a Creative Commons Attribution 4.0 International License