MUMPS, 77 76 bytes
r s,! f i=1:1:$l(s) s r=$a($e(s,i)) s b=" " f s b=(r#2)_b,r=r\2 i r=0 w b q
Example
REPL > r s w ! f i=1:1:$l(s) s r=$a($e(s,i)) s b=" " f s b=(r#2)_b,r=r\2 i r=0 w b q
Hello World!
1001000 1100101 1101100 1101100 1101111 100000 1010111 1101111 1110010 1101100 1100100 100001
Explanation
- M is great at keeping things to one line.
- We assign user input to variable
s
and write a newline (!
).
- For each character in our string, get the ASCII value with
$ascii()
and $extract()
(intrinsic functions can be abbreviated).
b
is meant to hold the binary value of our character, but we will quickly set it to be a space since we need that to separate the binary representation of each character.
- Now we enter an unconditional loop (two spaces after declaring the for loop). We prefix
b
with out ascii value (r
) modulo 2. You can do string operations on numbers in M.
- We then reduce our ascii value by half, disregarding the remainder (
\
is floor division)
- We do this until our ascii value hits 0, at which point we write our binary value