27
\$\begingroup\$

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.

\$\endgroup\$
  • 15
    \$\begingroup\$ +1 for making a challenge without story and other fripperies, straight to the point \$\endgroup\$ – bebe Jul 25 '14 at 18:21
  • 9
    \$\begingroup\$ @bebe Fictitious fantastical fripperies form half the fun. \$\endgroup\$ – qwr Jul 25 '14 at 23:59
  • \$\begingroup\$ 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. \$\endgroup\$ – Shaun Bebbers Apr 16 at 14:50

56 Answers 56

1
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ Because the language is newer than the question \$\endgroup\$ – krinistof Dec 23 '18 at 15:23
  • \$\begingroup\$ That's ok. No need to mark it as non-competing. \$\endgroup\$ – Wheat Wizard Dec 23 '18 at 19:51
  • \$\begingroup\$ Okay. Thanks! Removed \$\endgroup\$ – krinistof Dec 23 '18 at 22:19
1
\$\begingroup\$

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
\$\endgroup\$
1
\$\begingroup\$

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!
\$\endgroup\$
  • \$\begingroup\$ it's shorter if you evaluate s as is, encode it in decimal, and decode it from binary: `c$2/10\. s \$\endgroup\$ – ngn Feb 11 at 11:54
1
\$\begingroup\$

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!

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

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
\$\endgroup\$
  • \$\begingroup\$ use printf and you dont need to append \\c, saving 1 byte \$\endgroup\$ – roblogic Sep 3 at 0:29
1
\$\begingroup\$

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
\$\endgroup\$
1
\$\begingroup\$

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!

\$\endgroup\$
  • \$\begingroup\$ The question says the binary string will be stored in a variable called s? \$\endgroup\$ – Divy Sep 3 at 9:15
  • \$\begingroup\$ @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 \$\endgroup\$ – caird coinheringaahing Sep 3 at 9:26
0
\$\begingroup\$

Lua, 59 bytes

for k in s:gmatch'%d+'do io.write(s.char(tonumber(k,2)))end
\$\endgroup\$
  • \$\begingroup\$ To convert to a string rather than write to stdout: s:gsub('[10]+ ?',function(b)return s.char(tonumber(b,2))end). \$\endgroup\$ – cyclaminist Dec 24 '18 at 3:41
0
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

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))
\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

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!

\$\endgroup\$
0
\$\begingroup\$

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)
\$\endgroup\$
0
\$\begingroup\$

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']
\$\endgroup\$
0
\$\begingroup\$

Perl, 27 bytes

say pack"(B8)*",split' ',$s
\$\endgroup\$
  • \$\begingroup\$ Doesn't work. Note that each character is a group of 7 bytes. \$\endgroup\$ – dolmen Jan 3 at 8:33
0
\$\begingroup\$

Julia 1.0, 40 bytes

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

Try it online!

\$\endgroup\$
0
\$\begingroup\$

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
\$\endgroup\$
0
\$\begingroup\$

Common Lisp, 79 bytes

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

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Powershell, 48 bytes

Inspired by Joey's and fuandon's answers.

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

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
\$\endgroup\$
0
\$\begingroup\$

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.
\$\endgroup\$
0
\$\begingroup\$

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!

\$\endgroup\$
  • \$\begingroup\$ Invalid, needs to be either a full program or a function/lambda \$\endgroup\$ – ASCII-only Jan 3 at 7:56
  • \$\begingroup\$ 64 \$\endgroup\$ – ASCII-only Jan 3 at 7:57
  • \$\begingroup\$ Look at the javascript answer on the first page. It doesn't use a function. The question even says the variable is predefined. \$\endgroup\$ – Embodiment of Ignorance Jan 3 at 15:52
  • \$\begingroup\$ :/ then that's invalid too \$\endgroup\$ – ASCII-only Jan 4 at 0:45
  • \$\begingroup\$ But the OP says that the the binary string is already contained in a variable named s. \$\endgroup\$ – Embodiment of Ignorance Jan 4 at 0:47
0
\$\begingroup\$

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
\$\endgroup\$
0
\$\begingroup\$

Perl 5 -a, 24 bytes

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

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Julia 1.0, 44 bytes

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

Try it online!

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

Python, 46 bytes

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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