5
\$\begingroup\$

Your task is to write a program, which takes a binary input and converts it to plain text. For Example:

01100001 01100010 01100011 = "abc"

Rules

  • Input can come from STDIN, function arguments, command line argument, file, whatever suits you.
  • The program can be a function, full program, whatever works, really, as long as no external uncounted code/arguments are needed to run it.
  • You can assume that the input is valid.
  • The length of the input is not limited
  • You can assume that all blocks are seperated with whitespaces

Test Cases

01010100 01100101 01110011 01110100 = "Test"
01001000 01100101 01101100 01101100 01101111 = "Hello"
01010111 01101111 01110010 01101100 01100100 00001101 00001010 = "World"
01000101 01111000 01100001 01101101 01110000 01101100 01100101 00001101 00001010 = "Example"
01100010 01101001 01101110 00001101 00001010 = "bin"

This is , so the shortest solution wins.

\$\endgroup\$
8
  • 2
    \$\begingroup\$ Do mean we can or we must assume that all blocks are separated with whitespaces? \$\endgroup\$
    – Dada
    Mar 29, 2017 at 9:26
  • \$\begingroup\$ @Dada you can \$\endgroup\$
    – Tom291
    Mar 29, 2017 at 9:27
  • \$\begingroup\$ Why does this look related to the binary heart challenge? Most answer there is just a text to binary converter. \$\endgroup\$ Mar 29, 2017 at 10:22
  • \$\begingroup\$ What input formats are allowed? For example, an 8-column matrix of zeros and ones? A 2D array of chars? \$\endgroup\$
    – Luis Mendo
    Mar 29, 2017 at 10:25
  • 2
    \$\begingroup\$ Why is the dc answer accepted at all? The 05AB1E answer was first. \$\endgroup\$
    – Okx
    Mar 29, 2017 at 11:22

16 Answers 16

4
\$\begingroup\$

dc, 4 bytes

Needs as input the binary blocks with no separator. This is allowed by the OP.

2i?P

Try it online!

Explanation: using the "abc" example as test case

  • 2i? sets 2 as input radix, then reads the line (output radix is 10 by default). This converts the binary input to decimal: 011000010110001001100011 -> 6382179.
  • P is a special printing mode that assumes a base 256 number, using its "digits" as ASCII codes: 6382179 = (97 * 256 2) + (98 * 256 1) + (99 * 256 0) -> abc
\$\endgroup\$
3
\$\begingroup\$

05AB1E, 4 bytes

#CçJ

Uses the CP-1252 encoding. Try it online!

\$\endgroup\$
3
\$\begingroup\$

Perl 5, 15 bytes

14 bytes of code + -p flag.
The input must be supplied without final newline (with echo -n for instance).

$_=pack"B*",$_

Try it online!

\$\endgroup\$
2
\$\begingroup\$

JavaScript (ES6), 52 bytes

s=>String.fromCharCode(...s.split` `.map(s=>`0b`+s))
\$\endgroup\$
2
\$\begingroup\$

sed and dc, 23, 19 bytes

sed 's/.*/2i&P/'|dc
\$\endgroup\$
2
  • \$\begingroup\$ Why not just 's/.*/2i&P/'? \$\endgroup\$
    – Neil
    Mar 29, 2017 at 11:57
  • \$\begingroup\$ @Neil: good point \$\endgroup\$
    – Thor
    Mar 30, 2017 at 11:33
1
\$\begingroup\$

Pyth, 7 Bytes

smCid2c

Takes input as string.

Try it!

\$\endgroup\$
1
\$\begingroup\$

RProgN 2, 10 bytes

û{$-2Bo}r.

Explained

û          # Split the implicit input by Spaces.
 {     }r  # Replace each element in a list based on a function.
  $-2B     # Convert from Base 2
      o    # Get the ordinal of the character.
         . # Concatenate the list.

Try it online!

\$\endgroup\$
1
\$\begingroup\$

CJam, 10 bytes

l:~8/2fb:c

Try it online!

Explanation

l    e# Read input.
:~   e# Evaluate each character, resulting in a flat list of bits.
8/   e# Split into chunks of 8 bits.
2fb  e# Convert each from binary to an integer.
:c   e# Convert each integer to a character.
\$\endgroup\$
1
\$\begingroup\$

Mathematica, 50 bytes

FromCharacterCode[#~FromDigits~2&/@StringSplit@#]&
\$\endgroup\$
1
\$\begingroup\$

Röda, 36 35 bytes

{split|chr parseInteger(_,radix=2)}

Try it online!

This is an anonymous function that reads the input from the stream and prints the result.

It uses three builtins:

  • split splits the input at whitespaces
  • parseInteger converts a binary string to an integer value
  • chr converts an integer to a Unicode character
\$\endgroup\$
1
\$\begingroup\$

Gema, 33 characters

<D><s>=@int-char{@radix{2;10;$1}}

Sample run:

bash-4.3$ gema '<D><s>=@int-char{@radix{2;10;$1}}' <<< '01010000 01010000 01000011 01000111'
PPCG

Gema, 31 characters

Using no delimiter in the input.

<D8>=@int-char{@radix{2;10;$1}}

Sample run:

bash-4.3$ gema '<D8>=@int-char{@radix{2;10;$1}}' <<< '01010000010100000100001101000111'
PPCG
\$\endgroup\$
1
\$\begingroup\$

Python 2, 49 bytes

Try it online

lambda S:''.join(chr(int(c,2))for c in S.split())
\$\endgroup\$
5
  • 3
    \$\begingroup\$ Do you need the '0b'+ as well as the ,2? \$\endgroup\$
    – Neil
    Mar 29, 2017 at 9:21
  • \$\begingroup\$ @Neil Yes, chr() accepts only [0-255] integers and binary in python represented like 0bxxxxxx. So first I need to add 0b to binary blocks and then convert them into basic int to get chars. While converting, I need to specify base of input - 2. \$\endgroup\$ Mar 29, 2017 at 9:24
  • \$\begingroup\$ @ovs Oh, thats true! Unintuitive :) \$\endgroup\$ Mar 29, 2017 at 9:28
  • 1
    \$\begingroup\$ You can save 3 bytes by using list comprehension! Try it online! \$\endgroup\$ Mar 29, 2017 at 9:49
  • \$\begingroup\$ @KeerthanaPrabhakaran Good one! I've saved even more. Thanks \$\endgroup\$ Mar 29, 2017 at 9:56
1
\$\begingroup\$

Ruby, 36 characters

->b{b.gsub(/\d+ ?/){$&.to_i(2).chr}}

Sample run:

irb(main):001:0> ->b{b.gsub(/\d+ ?/){$&.to_i(2).chr}}['01010000 01010000 01000011 01000111']
=> "PPCG"
\$\endgroup\$
1
\$\begingroup\$

PHP <5.3, 53 Bytes

Idea by @manatwork

foreach(split(" ",$argv[1])as$v)echo chr(bindec($v));

PHP, 55 Bytes

I prefer cause it can work without spaces

foreach(str_split($argv[1],9)as$v)echo chr(bindec($v));

without spaces change 9 to 8 for 7 bits 7

foreach(explode(" ",$argv[1])as$v)echo chr(bindec($v));

PHP >=7, 62 Bytes

<?=join(($a=array_map)(chr,$a(bindec,explode(" ",$argv[1]))));
\$\endgroup\$
4
  • \$\begingroup\$ Is this PHP 7? Can't make it work due to a syntax error. \$\endgroup\$
    – manatwork
    Mar 29, 2017 at 10:22
  • 1
    \$\begingroup\$ Yes, but before you were able to use the deprecated split() function, so the length would be the same. \$\endgroup\$
    – manatwork
    Mar 29, 2017 at 10:51
  • \$\begingroup\$ Uhm, I also tried str_split(), but is the same length as explode(). And both are 2 characters longer than split(). [insert confused smiley here] \$\endgroup\$
    – manatwork
    Mar 29, 2017 at 11:17
  • \$\begingroup\$ @manatwork I think I was the only confused one. I like it not make post with deprecated functions. I mean str_split in combine with the foreach loop is the best solution cause it can handle other inputs with little changes \$\endgroup\$ Mar 29, 2017 at 11:30
1
\$\begingroup\$

C (clang), 43 bytes

f(**v){*v&&f(v+!!putchar(strtol(*v,0,2)));}

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ @ceilingcat, nice but we could have simply used strtol. \$\endgroup\$
    – jdt
    Oct 11, 2021 at 11:44
0
\$\begingroup\$

Jelly, unfinished

I still haven't figure out how to repeat for every line in STDIN, or get amount of lines in STDIN in Jelly. So far though, I came up for a solution for a single line/character.

ɠO_48ḄỌ

Any help is appreciated!

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.