# Convert a string of binary characters to the ASCII equivalents

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.

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

# 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.

• Because the language is newer than the question Dec 23 '18 at 15:23
• That's ok. No need to mark it as non-competing. Dec 23 '18 at 19:51
• Okay. Thanks! Removed Dec 23 '18 at 22:19

# 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


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

# 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!

# Zsh, 27 26 bytes

-1 from roblogic

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


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  • use printf and you dont need to append \\c, saving 1 byte Sep 3 '19 at 0:29 # 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  # 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! • The question says the binary string will be stored in a variable called s? – Divy Sep 3 '19 at 9:15 • @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 Sep 3 '19 at 9:26 # Lua, 59 bytes for k in s:gmatch'%d+'do io.write(s.char(tonumber(k,2)))end  • To convert to a string rather than write to stdout: s:gsub('+ ?',function(b)return s.char(tonumber(b,2))end). Dec 24 '18 at 3:41 # 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. # 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))  # 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. # 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! # 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)  # 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']  # Perl, 27 bytes say pack"(B8)*",split' ',$s

• Doesn't work. Note that each character is a group of 7 bytes. Jan 3 '19 at 8:33

# Julia 1.0, 40 bytes

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


Try it online!

# 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


# Common Lisp, 79 bytes

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


Try it online!

# Powershell, 48 bytes

Inspired by Joey's and fuandon's answers.

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


# 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


# Bash (Pure): 54

Pure and no fork!

• printf $v will render character. # 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! • Invalid, needs to be either a full program or a function/lambda Jan 3 '19 at 7:56 • 64 Jan 3 '19 at 7:57 • Look at the javascript answer on the first page. It doesn't use a function. The question even says the variable is predefined. Jan 3 '19 at 15:52 • :/ then that's invalid too Jan 4 '19 at 0:45 • But the OP says that the the binary string is already contained in a variable named s. Jan 4 '19 at 0:47 # 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  # Perl 5-a, 24 bytes say map chr oct"0b$_",@F


Try it online!

# Julia 1.0, 44 bytes

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


Try it online!

# Pip, 10 bytes

C*FB*(a^s)


Sadly can't use the -s flag here.

Try it online!

# Scala 2.12, 47 bytes

_ split " "map(_./:(0)(_*2+_-48)toChar)mkString


Try it online!

Splits on spaces, maps each string representing a binary number to an integer, turns into a Char, and then joins all those characters together. Scala 2.12 is required because of postfix operators.

# Python 3, 55 bytes

lambda s:"".join(map(lambda b:chr(int(b,2)),s.split()))


Try it online!

Or 46 bytes if we return an iterator instead of a string:

lambda s:map(lambda b:chr(int(b,2)),s.split())


Try it online!

# Python, 46 bytes

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