Convert a string of binary characters to the ASCII equivalents

27

2

Take a string of binary characters separated by a space, and convert it to an ASCII string.

For example...

1001000 1100101 1101100 1101100 1101111 100000 1010111 1101111 1110010 1101100 1100100

Would convert to...

Hello World

The binary string will be stored in a variable called s.

This is a code-golf challenge so the shortest solution wins.

James Williams

Posted 2014-07-25T17:48:33.337

Reputation: 1 735

Hello. Let's imagine that your technology is so advanced that it does not support ASCII. Would it be allowed to convert to the native character encoding, or would it have to convert ASCII to the native character encoding? I'm thinking ZX81 here. – Shaun Bebbers – 2019-04-16T14:50:54.180

16+1 for making a challenge without story and other fripperies, straight to the point – bebe – 2014-07-25T18:21:04.287

11@bebe Fictitious fantastical fripperies form half the fun. – qwr – 2014-07-25T23:59:57.413

Answers

10

Ruby, 36 32

s.split.map{|x|x.to_i(2).chr}*""

Or 31

s.gsub(/\w+ ?/){$&.to_i(2).chr}

Optimizations thanks to Chron

Kroltan

Posted 2014-07-25T17:48:33.337

Reputation: 517

1You could substitute .join"" for *"" to save a few characters. You could also do it with gsub instead of split+map+join, something like: s.gsub(/\w+ ?/){$&.to_i(2).chr} (31 chars). – Paul Prestidge – 2014-07-27T23:41:12.980

2s.gsub(/\d+./){$&.to_i(2).chr} works and it's 30 chars, I have no idea why it works though. The . shouldn't match the last time but it does. – addison – 2014-07-30T04:58:27.607

10

JavaScript (ES6) 49 55 56 64

s.replace(/\d+./g,x=>String.fromCharCode('0b'+x))

Edit Accepting @bebe suggestion - thanks
Edit2 Didn't know about binary numeric literal - thanks @kapep
Edit3 Wow 6 not 4 bytes saved thx @ETHproductions

Explanation as requested in comments

String.replace can take 2 arguments:

  • regular expression /\d+./g : one or more digits followed by one different character - the g flag specify to search the pattern more than once
  • a function, here specified in arrow format, where the argument (x) wil be the found string (the sequence of digits eventually followed by a space) and the value of the function is what is replaced (in this case, the single character from the code).

It's worth noting that the at the end of the string the regexp matches the sequence of digits without a trailing space, in this case the dot matches the last digit. Try '123'.match(/(\d+)./) to verify.

(Still) one of the more verbose pieces of javascript ever...
(Assignment to string s not counted)

var s='1001000 1100101 1101100 1101100 1101111 100000 1010111 1101111 1110010 1101100 1100100'
var x=
s.replace(/\d+./g,x=>String.fromCharCode('0b'+x))

console.log(x)

edc65

Posted 2014-07-25T17:48:33.337

Reputation: 31 086

I know this is an old answer, but you can change eval('0b'+x) to '0b'+x-0 to save 4 bytes. – ETHproductions – 2017-01-12T19:49:46.287

Firstly, it's 54. Secondly,s.split(' ').map(x=>String.fromCharCode(eval('0b'+x))) is also 54, but it returns char array – username.ak – 2017-01-12T20:56:34.127

@username.ak it's 55, count again. But it will be less an a couple of minutes – edc65 – 2017-01-12T21:26:10.003

@ETHproductions great, thanks – edc65 – 2017-01-12T21:31:49.967

Believe it or not, I've discovered you can actually remove the -0. I guess String.fromCharCode casts to number implicitly – ETHproductions – 2017-01-12T21:49:29.993

@ETHproductions unbelievable. (and again 4 stroked is 4) – edc65 – 2017-01-12T21:52:01.573

2s.replace(/\d+./g,x=>String.fromCharCode(parseInt(x,2))) 56 – bebe – 2014-07-25T22:35:12.160

2s.replace(/\d+./g,x=>String.fromCharCode(eval("0b"+x))) 55 – kapex – 2014-07-26T04:02:22.787

Can you please explain what the /\d+./g,x=>does? – izlin – 2014-07-28T05:38:21.347

1@izlin I added some explanation to the answer – edc65 – 2014-07-28T06:59:26.807

8

Bash+common linux utils, 25 bytes

dc<<<2i$s[Pz0\<m]dsmx|rev

dc explanation

  • push 2 to the stack; pop and use as input radix
  • push input string to stack (all values at once)
  • Define recursive macro m to:
    • pop, then print value as ASCII
    • push stack depth to stack
    • push 0 to stack
    • pop top 2 stack values; compare and call m macro if stack non-empty
  • duplicate top of stack (macro definition)
  • pop and save macro to m register
  • pop and execute macro

Because we push the whole binary string to the stack first, when we pop each value, we end up with the string reversed. So we use the rev utility to correct that.

Example usage:

$ s="1001000 1100101 1101100 1101100 1101111 100000 1010111 1101111 1110010 1101100 1100100"
$ dc<<<2i$s[Pz0\<m]dsmx|rev
Hello World
$ 

Digital Trauma

Posted 2014-07-25T17:48:33.337

Reputation: 64 644

7

PowerShell, 49

-join(-split$s|%{[char][convert]::toint32($_,2)})

EDIT: Didn't see the other PowerShell answer. But there is essentially only one way of solving this.

Joey

Posted 2014-07-25T17:48:33.337

Reputation: 12 260

7

C - 57 43 38/31

38 Byte version:

for(int*x=s;putchar(strtol(x,&x,2)););

Or only 31 bytes if s is a pointer:

while(putchar(strtol(s,&s,2)));

I don't think this is exactly how for and while loop are supposed to be used... but it works.

Ian D. Scott

Posted 2014-07-25T17:48:33.337

Reputation: 1 841

6

Powershell (52 49)

-join(-split$s|%{[char][convert]::ToInt16($_,2)})

Simple loop over binary string in $s. Having to include the [convert] kills my score though.

EDIT: There really is only one way of pulling this off in Powershell, wowie. Joey and I both got pretty much the same answer working independently!

Input:

1001000 1100101 1101100 1101100 1101111 100000 1010111 1101111 1110010 1101100 1100100

Output:

Hello World

fuandon

Posted 2014-07-25T17:48:33.337

Reputation: 309

1You can use the unary split operator, saving three characters (at which point our answers are identical ... big surprise ;-)). – Joey – 2014-07-25T22:06:43.717

@Joey Ohh, good point! Can't believe I missed that. You get the +1 from me for catching that, thanks! – fuandon – 2014-07-26T02:53:57.540

6

Pyth, 12

smCv+"0b"dPZ

Note that s is not a legal variable in Pyth, so I used Z instead.

Explanation:

        print(
s             sum(
m                 map(lambda d:
C                     chr(
v                         eval(
+"0b"d                         "0b"+d)),
P                     split(
Z                           Z))))

Example:

=Z"<the binary string from above>"smCv+"0b"dPZ
Hello World

isaacg

Posted 2014-07-25T17:48:33.337

Reputation: 39 268

I really like the formatting of your Explanation section. – Sparr – 2017-01-12T21:40:56.840

The fact it does not use whitespace in the formatting (here's looking at you Python) is a major +1 – Pharap – 2014-07-27T17:08:08.073

@Pharap Yeah, one way of looking at Pyth is it's Python with all of the elements that make it take more characters removed, like whitespace, parenthesis, multi-character tokens, etc. – isaacg – 2014-07-27T20:37:29.017

I don't know Pyth, but would it not save 4 characters to use id2 in place of v+"0b"d? In any case, this is both unreadable and cool. – DLosc – 2014-10-05T03:10:21.107

@DLosc I added that function to Pyth after this question was asked, in large part due to this question. It would be shorter with present day Pyth, but present day Pyth is not allowed for this challenge. – isaacg – 2014-10-05T03:38:13.083

I see. I wondered if it might be something like that. – DLosc – 2014-10-05T03:40:45.400

6

x86 machine code on DOS - 22 bytes

00000000  30 d2 b4 08 cd 21 2c 30  72 06 d0 e2 08 c2 eb f2  |0....!,0r.......|
00000010  b4 02 cd 21 eb ea                                 |...!..|

Since there are no real string variables in machine code (and in particular, no variables named "s") I settled for stdin as input.

NASM input:

    org 100h

section .text

start:
    xor dl,dl
loop:
    mov ah,8
    int 21h
    sub al,'0'
    jb print
    shl dl,1
    or dl,al
    jmp loop
print:
    mov ah,2
    int 21h
    jmp start

Matteo Italia

Posted 2014-07-25T17:48:33.337

Reputation: 3 669

5

Mathematica, 52 bytes

Ah, Mathematica's lovely function names

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

Martin Ender

Posted 2014-07-25T17:48:33.337

Reputation: 184 808

5

Perl 33 32

Edit: Updated solution, 32.

say$s=~s/\d+ ?/chr oct"0b$&"/rge

Previous solution (33):

$_=$s;say map{chr oct"0b$_"}split

or

say map{chr oct"0b$_"}split/ /,$s

Test:

perl -E '$s="1001000 1100101 1101100 1101100 1101111 100000 1010111 1101111 1110010 1101100 1100100";$_=$s;say map{chr oct"0b$_"}split'

hmatt1

Posted 2014-07-25T17:48:33.337

Reputation: 3 356

5

J (23)

u:;(#.@:("."0))&.>cut s

Test:

   s=:'1001000 1100101 1101100 1101100 1101111 100000 1010111 1101111 1110010 1101100 1100100'
   u:;(#.@:("."0))&.>cut s
Hello World

Explanation:

                  cut s    NB. split S on spaces
   (          )&.>         NB. for each element
        ("."0)             NB. evaluate each character
      @:                   NB. and
    #.                     NB. convert bitstring to number
  ;                        NB. unbox each number
u:                         NB. convert to ASCII

marinus

Posted 2014-07-25T17:48:33.337

Reputation: 30 224

4

Python shell  44  40 chars

''.join(chr(int(x,2))for x in s.split())

Thanks for the help Griffin.

James Williams

Posted 2014-07-25T17:48:33.337

Reputation: 1 735

''.join(chr(int(x,2))for x in s.split()) – Griffin – 2014-07-25T17:55:46.893

4

Golfscript - 21

' '/{1/{~}%2base}%''+

You can test it here.

Kyle McCormick

Posted 2014-07-25T17:48:33.337

Reputation: 3 651

4

APL (15)

⎕UCS{2⊥⍎¨⍕⍵}¨⍎s

Test:

      s←'1001000 1100101 1101100 1101100 1101111 100000 1010111 1101111 1110010 1101100 1100100'
      ⎕UCS{2⊥⍎¨⍕⍵}¨⍎s
Hello World

Explanation:

  • ⍎s: evaluate s, turning it into an array of integers. Arrays are written as numbers separated by spaces, so this splits s.
  • {...: for each element:
    • ⍕⍵: turn the number back into a string
    • ⍎¨: evaluate each individual digit, giving a bitstring
    • 2⊥: base-2 decode, giving the numbers
  • ⎕UCS: get the character for each number

marinus

Posted 2014-07-25T17:48:33.337

Reputation: 30 224

Unless otherwise specified you should count bytes instead of characters: http://codegolf.stackexchange.com/tags/code-golf/info :)

– Averroes – 2014-07-28T09:36:49.620

1

@Averroes: the APL charset fits in a byte with room to spare: http://frankenstein.d-n-s.org.uk/~marinus/aplcharset.png

– marinus – 2014-07-31T12:04:40.493

Ah, I didn't know it. My bad. Sorry! – Averroes – 2014-07-31T12:10:47.907

4

PHP (61)

<?=join(array_map('chr',array_map('bindec',explode(' ',$s))))

Christoph

Posted 2014-07-25T17:48:33.337

Reputation: 1 489

foreach(str_split($s,8)as$v)echo chr(bindec($v)); – Jörg Hülsermann – 2017-03-29T11:15:25.177

@JörgHülsermann the encoding may skip leading zeros therefore str_split($s,8) won't work. foreach(explode(' ',$s)as$v)echo chr(bindec($v)); would be valid but I don't plan to edit one of my first PPGC answers that clearly isn't really golfed anyway. Thank you anyway! – Christoph – 2017-03-29T12:46:14.560

4

Bacchus, 25 bytes

S,' 'j:A=(Ö,2,10b:c),A¨

Explanation:

S,' 'j split the String S by the empty space and converts it to a block (some sort of array).

:A= get the previous Block and assing it to A variable.

(),A¨ for Each element in A

Ö,2,10b Read the current element (represent by Ö) in base 2 and transform it to base 10.

:c get the previous value and print as char

Averroes

Posted 2014-07-25T17:48:33.337

Reputation: 3 771

3

GolfScript 23

' '/{[{49=}/]2base}%''+

Online test here.

Cristian Lupascu

Posted 2014-07-25T17:48:33.337

Reputation: 8 369

3

C - 63

since C has no base 2 converter in standard library: test here
edit: there is, i'm just too stupid to know about it

r;f(char*s){for(;*s;(*s|32)-32||(putchar(r),r=0))r=2*r|*s++&1;}

bebe

Posted 2014-07-25T17:48:33.337

Reputation: 3 916

3

Run Length Encoded Brainfuck, 49 bytes

Since there are no variables in Brainfuck, I just used standard input and output instead.

The code 32+ should be interpreted as 32 +s by the interpreter. Just replace them manually if your interpreter doesn't support RLE.

>,[32->+<[16-<[>++<-]>[<+>-]>-<]>[<<.[-]>>-]<,]<.

Expanded (non-RLE) version: (91 bytes)

>,[-------------------------------->+<[----------------<[>++<-]>[<+>-]>-<]>[<<.[-]>>-]<,]<.

The code assumes that EOF is encoded as 0.

Explanation

The following layout is used:

+---+---+------+
| x | a | flag |
+---+---+------+

Where x is the ASCII byte to be printed, a is the a character from standard input and flag is 1 if a was a space.

>,            Read a character a into the second cell
[             While not EOF: 
  32-           Decrease a by 32 (a -= ' ')
  >+<           Set the flag to 1 
  [             If a was not a space:
    16-           Decrease by 16 more ('0' == 32+16)
    <[>++<-]      a += 2*x
    >[<+>-]       Move it back (x = a)
    >-<           Reset the flag, it was not a space.
  ]>
  [             If a was a space (flag == 1):
    <<.[-]        Print and reset x
    >>-           Reset the flag
  ]
  <,            Read the next caracter a
]
<.            Print the last character x

Hjulle

Posted 2014-07-25T17:48:33.337

Reputation: 479

Is "Run Length Encoded Brainfuck" an actual separate language? I can't find an interpreter. – mbomb007 – 2017-03-29T15:31:18.653

1+1 Because everything should have a brainfuck implementation. – Pharap – 2014-07-27T17:09:17.193

3

Java 8 : 60 bytes

Using lambdas in Java 8 (75 bytes):

Arrays.stream(s.split(" ")).reduce("",(a,b)->a+(char)Byte.parseByte(b,2));

And if you allow static imports (which some here used) it is (61 bytes):

stream(s.split(" ")).reduce("",(a,b)->a+(char)parseInt(b,2))

A tiny bit shorter version using for loop (60 bytes):

for(String n:s.split(" ")){out.print((char)parseInt(n,2));}

Roy van Rijn

Posted 2014-07-25T17:48:33.337

Reputation: 1 082

2What, they actually made a replacement for the monstrosity known as anonymous class? Awesome. – seequ – 2014-07-28T08:53:41.943

@Sieg yes Java has lambdas/closures now as well, but it is mostly synthetic sugar on top of anonymous classes... (obviously) – Roy van Rijn – 2014-07-28T12:39:27.827

3

Clojure 63 (or 57)

The all-Clojure impl:

(apply str(map #(char(read-string(str"2r"%)))(re-seq #"\d+"s)))

With Java interop:

(apply str(map #(char(Long/parseLong % 2))(.split s" ")))

REPL session:

golf> (def s "1001000 1100101 1101100 1101100 1101111 100000 1010111 1101111 1110010 1101100 1100100")
#'golf/s
golf> (apply str(map #(char(read-string(str"2r"%)))(re-seq #"\d+"s)))
"Hello World"
golf> (apply str(map #(char(Long/parseLong % 2))(.split s" ")))
"Hello World"

YosemiteMark

Posted 2014-07-25T17:48:33.337

Reputation: 213

3

QBasic, 103

s$=s$+" ":FOR i=1 TO LEN(s$):c$=MID$(s$,i,1):IF c$=" "THEN n=0:r$=r$+CHR$(n)ELSE n=n*2+VAL(c$)
NEXT:?r$

What? We have no fancy binary-to-decimal functions here. Do it yourself!

I'm counting the newline (which I think is necessary to get the if-then-else without an END IF) as one byte, per this meta post. I don't know whether QB64 on Windows would accept a code file that way or not. Probably doesn't much matter.

DLosc

Posted 2014-07-25T17:48:33.337

Reputation: 21 213

2

Haskell -- 48 (+13 imports (?))

Here's my first golf attempt in Haskell.

map(chr.foldl1((+).(*2)).map digitToInt)$words s

You need to import Data.Char

usage (in ghci):

Prelude> :m +Data.Char
Prelude Data.Char> let s = "1001000 1100101 1101100 1101100 1101111 100000 1010111 1101111 1110010 1101100 1100100"
Prelude Data.Char> map(chr.foldl1((+).(*2)).map digitToInt)$words s
"Hello World"

Explanation:

map(chr.foldl1((+).(*2)).map digitToInt)$words s
                                        $words s -- split s on spaces into a list
                         map digitToInt          -- convert each digit in input string to int
              ((+).(*2))                         -- a function that multiplies its first 
-- argument by 2, then adds the second argument
        foldl1((+).(*2)).map digitToInt          -- fold the above over the list of ints: 
-- in other words this is a function that reads strings as binary and gives the value as int
   (chr.foldl1((+).(*2)).map digitToInt)         -- cast to character
map(chr.foldl1((+).(*2)).map digitToInt)$words s -- map our function over the list of words

ballesta25

Posted 2014-07-25T17:48:33.337

Reputation: 65

Hopefully I've followed scoring conventions correctly regarding imports. Feel free to edit in a correction if I haven't. I treated the ":m +Data.Char" needed to import in ghci as 13. – ballesta25 – 2014-07-26T02:30:40.570

2

NodeJS62

Buffer(s.split(' ').map(function(a){return parseInt(a,2)}))+''

PHP75

array_reduce(explode(' ', $b),function($a,$b){return $a.chr(bindec($b));});

c.P.u1

Posted 2014-07-25T17:48:33.337

Reputation: 1 049

@c.P.u1 they work for me – username.ak – 2017-01-12T21:03:59.570

how do you run nodejs with es6 compatibility? the latest one still does not support lambda functions for me – bebe – 2014-07-26T14:35:07.230

@bebe Arrow functions are implemented in v8 but they aren't integrated with node yet. I'll edit my post. – c.P.u1 – 2014-07-26T15:05:57.317

2

JavaScript 111

This does the number conversion without parseInt or eval. Reading the string backwards and counting bits it set's bit x if it's a one. When a space is found a the number is converted to a char and a new 0 number is started for setting bits.

x=n=0,w='',s=' '+s
for(i=s.length;i--;){m=s[i]
if(m==1)n|=1<<x
x++
if(m==' ')w=String.fromCharCode(n)+w,n=x=0
}

wolfhammer

Posted 2014-07-25T17:48:33.337

Reputation: 1 219

Invalid, needs to be a function or full program (one that takes input) – ASCII-only – 2019-01-04T00:46:46.717

1+1 for doing it the old-fashioned way--inspired me to write a QBasic version, too. – DLosc – 2014-10-05T03:29:16.937

2

Groovy 64

{it.split(" ").collect{Integer.parseInt(it,2) as char}.join("")}

markusw

Posted 2014-07-25T17:48:33.337

Reputation: 121

2

CJam, 11 bytes

NS/{:~2bc}/

s isn't a legal variable name in CJam, so I chose N instead.

Try it online.

Example run

$ cjam <(echo '
> "1001000 1100101 1101100 1101100 1101111 100000 1010111 1101111 1110010 1101100 1100100"
> :N;
> NS/{:~2bc}/
> '); echo
Hello World

How it works

NS/            " Split N at spaces.                            ";
   {     }/    " For each chunk:                               ";
    :~         "   Evaluate each character ('0' ↦ 0, '1' ↦ 1). ";
      2b       "   Convert from base 2 array to integer.       ";
        c      "   Cast to character.                          ";

Dennis

Posted 2014-07-25T17:48:33.337

Reputation: 196 637

1

05AB1E, 4 bytes (non-competing)

Saved 3 bytes thanks to @Emigna

#CçJ

Try it online!

Magic Octopus Urn

Posted 2014-07-25T17:48:33.337

Reputation: 19 422

1#CçJ also works. Although in both this and your version you need ð¡ instead of # if the input can be only 1 char. – Emigna – 2017-01-12T14:58:02.290

1

GNU Sed, 19 bytes

Inspired by an excellent @Digital Trauma answer.

Golfed

s/\w*/dc -e2i&P;/eg

Test

echo 1001000 1100101 1101100 1101100 1101111 100000 1010111 1101111 1110010 1101100 1100100|\
sed 's/\w*/dc -e2i&P;/eg'

Hello World

zeppelin

Posted 2014-07-25T17:48:33.337

Reputation: 7 884

1

Pyth, 7 bytes (non-competing)

smCid2c

Takes input as string.

Try it!

KarlKastor

Posted 2014-07-25T17:48:33.337

Reputation: 2 352

Marked non-competing because the language is newer than the challenge. – mbomb007 – 2017-03-29T15:24:23.390

1

05AB1E (legacy), 4 bytes

#CçJ

Try it online!

Explanation:

#CçJ
#   : Split implicit input at spaces
 C  : Convert every element to int
  ç : chr(n)
   J: Join elements, push it to the stack for printing

Edit: I noticed I perfectly duplicated a previous answer by my own, but keep it.

krinistof

Posted 2014-07-25T17:48:33.337

Reputation: 431

Because the language is newer than the question – krinistof – 2018-12-23T15:23:05.230

That's ok. No need to mark it as non-competing.

– Post Rock Garf Hunter – 2018-12-23T19:51:18.667

Okay. Thanks! Removed – krinistof – 2018-12-23T22:19:11.433

1

K4, 18 bytes

Solution:

"c"$2/:'"1"=" "\:s

Example:

q)s:"1001000 1100101 1101100 1101100 1101111 100000 1010111 1101111 1110010 1101100 1100100"
q)k)"c"$2/:'"1"=" "\:s
"Hello World"

Explanation:

"c"$2/:'"1"=" "\:s / the solution
                 s / the input
            " "\:  / split (\:) on " "
        "1"=       / equal to "1" ?
    2/:'           / join (/:) each (') from base-2
"c"$               / cast to characters

streetster

Posted 2014-07-25T17:48:33.337

Reputation: 3 635

1

K (ngn/k), 15 11 bytes

Solution:

`c$2/10\. s

Try it online!

Explanation:

`c$2/10\. s / the solution
          s / input s
        .   / value "1100" => 1100
     10\    / convert to base 10
   2/       / convert from base 2
`c$         / cast to character
  • -4 bytes thanks to ngn!

streetster

Posted 2014-07-25T17:48:33.337

Reputation: 3 635

it's shorter if you evaluate s as is, encode it in decimal, and decode it from binary: \c$2/10. s`

– ngn – 2019-02-11T11:54:45.477

1

Zsh, 27 26 bytes

-1 from roblogic

for c
printf ${(#):-2\#$c}

Try it online! Try it online!

The (#) flag causes arithmetic expansion to be done to the contents of the parameter -- which in this case is empty, falling back to 2\#$c. This parses $c as a binary number, converting it to a character.


This relies on flexible I/O, each group of bits in its own parameter. This is what the pure bash answer does as well. To parse a single string, +6 bytes are needed in the first line: (Example)

for c ($=s)     # for string in $s
for c ($=1)     # for string passed whole to function

GammaFunction

Posted 2014-07-25T17:48:33.337

Reputation: 2 838

use printf and you dont need to append \\c, saving 1 byte – roblogic – 2019-09-03T00:29:46.573

1

Python 3, 33 bytes

bytes(int(c,2)for c in s.split())

Try it online!

This solution outputs a byte string in Python. If it is required to return a regular Python string:

Python 3, 42 bytes

bytes(int(c,2)for c in s.split()).decode()

Try it online!

Joel

Posted 2014-07-25T17:48:33.337

Reputation: 1 691

1

brainfuck, 74 61 bytes

,[>++++[>++++<-]>[<+<-->>-]<<[>[<->-]<<[>++<-]]<[.[-]]>>>,]<.

Try it online!

Explanation:

[tape: char, 'input, 16, 16]
[      else]

,[                      for each input
  >++++[>++++<-]>           set const 16
  [<+<-->>-]                decrement input by 32
  <<[                       if not input = space
    >[<->-]                     decrement input by 16
    <<[>++<-]                   add double of char to input
  ]
  <[                        else
    .                           print character
    [-]                         delete character
  ]
  >>>,                      input next character
]
<.                      print last character

Dorian

Posted 2014-07-25T17:48:33.337

Reputation: 1 521

1

Python 3, 43 bytes

for i in s.split():print(end=chr(int(i,2)))

fixed a blunder! Thanks to @caird-coinheringaahing XD

Try it online!

Divy

Posted 2014-07-25T17:48:33.337

Reputation: 51

The question says the binary string will be stored in a variable called s? – Divy – 2019-09-03T09:15:37.233

@JoKing Have you read the challenge? OP's code doesn't work (Try it online!), but not because of the reason you've stated. However this does work

– caird coinheringaahing – 2019-09-03T09:26:37.243

0

REXX, 56 bytes (or 15)

o=
do w=1 to words(s)
  o=o||x2c(b2x(word(s,w)))
  end
say o

Alternatively, if the input values are an even 8 bits instead of varying in length as in the example, this may be shortened to just one line:

say x2c(b2x(s))

idrougge

Posted 2014-07-25T17:48:33.337

Reputation: 641

0

AHK, 115 bytes

Loop,Parse,s," "
{x:=D(A_LoopField)
Send % Chr(x)
}
D(x){
Return (StrLen(x)>1?D(SubStr(x,1,-1))<<1:0)|SubStr(x,0)
}

The loop parses the string using a space as the delimeter.
The function D(x) converts x from binary to decimal.
The Chr function returns the ASCII character with the value x.

Engineer Toast

Posted 2014-07-25T17:48:33.337

Reputation: 5 769

0

Tcl, 46 bytes

proc B s {binary f B* [regsub -all " " $s ""]}

Try it online!

Tcl, 51 bytes

proc B s {binary format B* [regsub -all " " $s ""]}

Try it online!

sergiol

Posted 2014-07-25T17:48:33.337

Reputation: 3 055

0

q/kdb+, 22 bytes

Solution:

10h$0b sv'"1"=-8$" "vs

Example:

q)10h$0b sv'"1"=-8$" "vs"1001000 1100101 1101100 1101100 1101111 100000 1010111 1101111 1110010 1101100 1100100"
"Hello World"

Explanation:

Split string on " ", left-pad each to 8 characters, return boolean list where each string is equal to "1", convert each 8-bit list to a byte and then cast to a string:

10h$0b sv'"1"=-8$" "vs / the solution
                 " "vs / split (vs) on " "
              -8$      / pad ($) to 8-long, negative means left-pad
          "1"=         / is item in each list equal to 1, returns true (1) or false (0) for each
    0b sv'             / convert each (') from boolean list to bytes
10h$                   / cast ($) to character type (10h)

streetster

Posted 2014-07-25T17:48:33.337

Reputation: 3 635

0

Deorst, 11 bytes

o k`]mxmcE;

Try it online!

How it works

Example input: 1001000 1100101

o<space>    - Push a  space;    STACK = [' ', '1001000 1100101']
  k         - Turn off sort;    STACK = [' ', '1001000 1100101']
   `        - Split;            STACK = [['1001000', '1100101']]
    ]       - Flatten;          STACK = ['1001000', '1100101']
     mx     - Map from binary;  STACK = [72, 101]
       mc   - Map to character; STACK = ['H', 'e']
         E; - Concatenate;      STACK = ['H', 'e', 'He']

caird coinheringaahing

Posted 2014-07-25T17:48:33.337

Reputation: 13 702

0

Perl, 27 bytes

say pack"(B8)*",split' ',$s

scytale

Posted 2014-07-25T17:48:33.337

Reputation: 1

Doesn't work. Note that each character is a group of 7 bytes. – dolmen – 2019-01-03T08:33:47.650

0

Julia 1.0, 40 bytes

join(Char.(parse.(Int,split(s),base=2)))

Try it online!

Rustem B.

Posted 2014-07-25T17:48:33.337

Reputation: 51

0

Gol><>, 27 24 bytes

IWaSD$|rlF2LX*}|lR+oiE;~

Try it online!

Explanation:

I                         //Read the next number as integer
 W                        //Loop to extract single digits
  aSD                     //  Perform a mod & div by 10 on the current number
     $|                   //  Repeat until the number is 0
       r                  //Reverse the stack to go from LSB to MSB
        lF                //Loop over all single bits and replace all 1's with their corresponding value
          2LX             //  Push 2^x where x is the number of the current bit -> value of topmost bit
             *            //  Multiply the value of the topmost bit with the bit itself
              }|          //  Shift right to get the next bit
                lR+       //Add all values on the stack together
                   o      //Output the decimal value as a char
                    iE;~  //Check for EOF if true terminate

Gegell

Posted 2014-07-25T17:48:33.337

Reputation: 81

0

Common Lisp, 79 bytes

(map'string'code-char(let((*read-base* 2))(read-from-string(format()"(~a)"s))))

Try it online!

Renzo

Posted 2014-07-25T17:48:33.337

Reputation: 2 260

0

Powershell, 48 bytes

Inspired by Joey's and fuandon's answers.

-join(-split$s|%{[char][convert]::ToByte($_,2)})

mazzy

Posted 2014-07-25T17:48:33.337

Reputation: 4 832

0

Perl 6, 24 bytes

*.words>>.&{:2($_)}.chrs

Try it online!

Anonymous Whatever lambda that takes a string and returns a string.

Explanation

*.words                   # Split by spaces
       >>.&{      }       # Map each word to
            :2($_)        # Parsing it as binary
                   .chrs  # Convert to characters

Jo King

Posted 2014-07-25T17:48:33.337

Reputation: 38 234

0

Bash (Pure): 54

Pure and no fork!

f(){ for a;do printf -vv \\%o $[2#$a];printf $v;done;}

f 1001000 1100101 1101100 1101100 1101111 100000 1010111 1101111 1110010 1101100 1100100
Hello World

Explanation

  • f() declare function f
  • for a;do ... done take each argument successively and attrib them to variable a in loop
  • $[2#$a] convert binary to decimal
  • printf -vv \\%o $... convert decimal to octal, preceded by backslash \ for next printf and attribute this to variable v.
  • printf $v will render character.

F. Hauri

Posted 2014-07-25T17:48:33.337

Reputation: 2 654

0

C# (Visual C# Interactive Compiler), 61 bytes

s.Split(" ").Aggregate("",(a,b)=>a+(char)Convert.ToByte(b,2))

Splits the binary string, and adds each byte into the string. You need to have the seed for aggregate, or else the first byte won't be parsed.

Try it online!

Embodiment of Ignorance

Posted 2014-07-25T17:48:33.337

Reputation: 7 014

Invalid, needs to be either a full program or a function/lambda – ASCII-only – 2019-01-03T07:56:51.180

64 – ASCII-only – 2019-01-03T07:57:38.167

Look at the javascript answer on the first page. It doesn't use a function. The question even says the variable is predefined. – Embodiment of Ignorance – 2019-01-03T15:52:56.380

:/ then that's invalid too – ASCII-only – 2019-01-04T00:45:58.243

But the OP says that the the binary string is already contained in a variable named s. – Embodiment of Ignorance – 2019-01-04T00:47:12.877

Oh wait >_> didn't see that – ASCII-only – 2019-01-04T01:02:42.237

0

Japt -P, 4 bytes

¸®Íd

Try it

¸®Íd     :Implicit input of string
¸        :Split on spaces
 ®       :Map
  Í      :  Convert from binary string to integer
   d     :  Get character at that codepoint
         :Implicitly join & output

Shaggy

Posted 2014-07-25T17:48:33.337

Reputation: 24 623

0

Perl 5 -a, 24 bytes

say map chr oct"0b$_",@F

Try it online!

Xcali

Posted 2014-07-25T17:48:33.337

Reputation: 7 671

0

Julia 1.0, 44 bytes

prod(Char.(parse.(Int,split(s," "),base=2)))

Try it online!

gggg

Posted 2014-07-25T17:48:33.337

Reputation: 1 715

0

Lua, 59 bytes

for k in s:gmatch'%d+'do io.write(s.char(tonumber(k,2)))end

Jim Bauwens

Posted 2014-07-25T17:48:33.337

Reputation: 81

To convert to a string rather than write to stdout: s:gsub('[10]+ ?',function(b)return s.char(tonumber(b,2))end). – cyclaminist – 2018-12-24T03:41:59.907

0

Erlang, 45 Bytes

[binary_to_integer(X,2)||X<-re:split(S," ")].

Strings are represented as a list of integers in Erlang. This returns a list of ASCII codes which, when printed, will be represented as a string if all ASCII codes are printable characters.

c.P.u1

Posted 2014-07-25T17:48:33.337

Reputation: 1 049

-1

Python, 46 bytes

''.join(map(lambda x:chr(int(x,2)),s.split()))

globby

Posted 2014-07-25T17:48:33.337

Reputation: 1 132