35
\$\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

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

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

69 Answers 69

1 2
3
0
\$\begingroup\$

Julia 1.0, 44 bytes

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

Try it online!

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

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!

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

TECO, 35 bytes

Z+1<0A"DQn*2#(0A-48)Un|QnI^[0Un'1:D>

^[ stands for esc.

Explanation

I1001000 1100101 1101100 1101100 1101111 100000 1010111 1101111 1110010 1101100 1100100^[
! insert input string to the buffer !
J     ! reset to zero point !
Z+1<  ! loop over all chars !
0A"D  ! if the current char is a digit !
Qn*2#(0A-48)Un ! append the bit to register n !
D     ! remove current char !
|     ! else !
QnI^[ ! insert value of register n as ASCII !
0Un   ! clear n !
1:D   ! delete one char without signaling error !
'     ! end if !
>     ! end loop !
HT    ! type out result !
\$\endgroup\$
0
\$\begingroup\$

JavaScript, 64 bytes

s=>s.replace(/\d+./g,c=>eval(`'\\x${('0b'+c|0).toString(16)}'`))

Try it online!

Trying something different to this JS answer, not sure if it can be shorter.

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

Python 3, 48 44 bytes

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

Try it online!

previous answer,

[print(end=chr(eval('0b'+i)))for i in s.split()]

Try it online!

Learnt the print(end=...) trick in the "Scream the loudest" challenge. Pretty neat.

As far as I know, there is no direct conversion to ascii from binary string, so we have to suffer the '0b' in the eval.

Thanks to this python answer for giving int(i,2)

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

Python 3, 53 bytes

lambda x:[print(end=chr(int(i,2)))for i in x.split()]

Try it online!

Used the int(i,2) to convert number from base 2 to 10. In-built functions for the win.

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

Octave, 30 bytes

Nothing too fancy, but it's relatively short

@(x)['',bin2dec(strsplit(x))']

Try it online!

@(x)                            % Take x as input
@(x)            strsplit(x)     % split string at spaces and create a cell array
@(x)    bin2dec(strsplit(x))    % Convert each row to integers
@(x)    bin2dec(strsplit(x))'   % Transpose into column vector
@(x)['',bin2dec(strsplit(x))']  % Concatenate with the empty string to cast to chars
\$\endgroup\$
0
\$\begingroup\$

Thunno 2 J, 3 bytes

OḂC

Attempt This Online!

But, if a list of strings was an allowed input format:

Thunno 2 J, 2 bytes

ḂC

Attempt This Online!

Explanation

OḂC  # Implicit input
O    # Split on spaces
 Ḃ   # Convert from binary
  C  # Convert to characters
     # Implicit output
\$\endgroup\$
-1
\$\begingroup\$

Python, 46 bytes

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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