Convert Binary to Text

5

0

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.

Tom291

Posted 2017-03-29T08:55:25.590

Reputation: 313

Question was closed 2017-03-29T10:54:47.520

2Do mean we can or we must assume that all blocks are separated with whitespaces? – Dada – 2017-03-29T09:26:43.817

@Dada you can – Tom291 – 2017-03-29T09:27:27.210

Why does this look related to the binary heart challenge? Most answer there is just a text to binary converter. – Matthew Roh – 2017-03-29T10:22:22.460

What input formats are allowed? For example, an 8-column matrix of zeros and ones? A 2D array of chars? – Luis Mendo – 2017-03-29T10:25:57.657

Can I take the binary blocks with no separator (no space)? – seshoumara – 2017-03-29T10:37:17.193

@seshoumara Yes, you can. – Tom291 – 2017-03-29T10:59:52.887

You should really wait a few days to weeks before picking an accepted answer. What if someone manages shorter than 4 bytes in a few hours or days? – devRicher – 2017-03-29T11:17:45.440

2Why is the dc answer accepted at all? The 05AB1E answer was first. – Okx – 2017-03-29T11:22:31.007

Answers

3

05AB1E, 4 bytes

#CçJ

Uses the CP-1252 encoding. Try it online!

Adnan

Posted 2017-03-29T08:55:25.590

Reputation: 41 965

4

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

seshoumara

Posted 2017-03-29T08:55:25.590

Reputation: 2 878

3

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!

Dada

Posted 2017-03-29T08:55:25.590

Reputation: 8 279

2

JavaScript (ES6), 52 bytes

s=>String.fromCharCode(...s.split` `.map(s=>`0b`+s))

Neil

Posted 2017-03-29T08:55:25.590

Reputation: 95 035

2

sed and dc, 23, 19 bytes

sed 's/.*/2i&P/'|dc

Thor

Posted 2017-03-29T08:55:25.590

Reputation: 2 526

Why not just 's/.*/2i&P/'? – Neil – 2017-03-29T11:57:29.427

@Neil: good point – Thor – 2017-03-30T11:33:13.263

1

Pyth, 7 Bytes

smCid2c

Takes input as string.

Try it!

KarlKastor

Posted 2017-03-29T08:55:25.590

Reputation: 2 352

1

Python 2, 49 bytes

Try it online

lambda S:''.join(chr(int(c,2))for c in S.split())

Dead Possum

Posted 2017-03-29T08:55:25.590

Reputation: 3 256

3Do you need the '0b'+ as well as the ,2? – Neil – 2017-03-29T09:21:11.087

@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. – Dead Possum – 2017-03-29T09:24:56.483

@ovs Oh, thats true! Unintuitive :) – Dead Possum – 2017-03-29T09:28:26.723

1

You can save 3 bytes by using list comprehension! Try it online!

– Keerthana Prabhakaran – 2017-03-29T09:49:08.773

@KeerthanaPrabhakaran Good one! I've saved even more. Thanks – Dead Possum – 2017-03-29T09:56:15.907

1

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!

ATaco

Posted 2017-03-29T08:55:25.590

Reputation: 7 898

1

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.

Martin Ender

Posted 2017-03-29T08:55:25.590

Reputation: 184 808

1

Mathematica, 50 bytes

FromCharacterCode[#~FromDigits~2&/@StringSplit@#]&

Martin Ender

Posted 2017-03-29T08:55:25.590

Reputation: 184 808

1

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

fergusq

Posted 2017-03-29T08:55:25.590

Reputation: 4 867

1

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

manatwork

Posted 2017-03-29T08:55:25.590

Reputation: 17 865

1

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"

manatwork

Posted 2017-03-29T08:55:25.590

Reputation: 17 865

1

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]))));

Jörg Hülsermann

Posted 2017-03-29T08:55:25.590

Reputation: 13 026

Is this PHP 7? Can't make it work due to a syntax error. – manatwork – 2017-03-29T10:22:28.570

1Yes, but before you were able to use the deprecated split() function, so the length would be the same. – manatwork – 2017-03-29T10:51:15.987

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] – manatwork – 2017-03-29T11:17:24.023

@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 – Jörg Hülsermann – 2017-03-29T11:30:07.177

1

C, 106 Bytes

main(a,b,c,d,e,f)char**b,*f;{for(c=1;c<a;c++){for(f=b[c],e=7,d=0;*f;f++,e--)d+=(*f-48)*1<<e;putchar(d);}}

Johan du Toit

Posted 2017-03-29T08:55:25.590

Reputation: 1 524

0

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!

devRicher

Posted 2017-03-29T08:55:25.590

Reputation: 1 609