Logo

Programming-Idioms

From the string s consisting of 8n binary digit characters ('0' or '1'), build the equivalent array a of n bytes.
Each chunk of 8 binary digits (2 possible values per digit) is decoded into one byte (256 possible values).
New implementation

Type ahead, or select one

Explain stuff

To emphasize a name: _x → x

Please be fair if you are using someone's work

You agree to publish under the CC-BY-SA License

Be concise.

Be useful.

All contributions dictatorially edited by webmasters to match personal tastes.

Please do not paste any copyright violating material.

Please try to avoid dependencies to third-party libraries and frameworks.

Other implementations
#include <stdlib.h>
#include <string.h>
unsigned char *a = calloc(strlen(s) / 8, 1);
for(int i = 0; i < strlen(s); ++i){
  a[i / 8] |= (s[i] == '1') << (7 - i % 8);
}

If s is not 8n characters long or contains characters orher than '0' and '1', correct behavior is not guaranteed.