26
\$\begingroup\$

This is a code golf challenge. Just like the title says, write a program to covert a string of ascii characters into binary.

For example:

"Hello World!" should turn into 1001000 1100101 1101100 1101100 1101111 100000 1010111 1101111 1110010 1101100 1100100 100001.

Note: I am particularly interested in a pyth implementation.

\$\endgroup\$
9
  • 1
    \$\begingroup\$ We had the reversed asked: codegolf.stackexchange.com/questions/35096/… \$\endgroup\$
    – hmatt1
    Commented Jan 23, 2015 at 0:57
  • 1
    \$\begingroup\$ I noticed that. There's an anecdote for why I asked this question. I encouraged my friend to learn programming, and he took a java class last summer where each student had to pick a project. He told me he wanted to translate text to binary, which I then did (to his dismay) in python 3 in 1 line (a very long line). I find it incredible that his project idea can be distilled down to 8 bytes. \$\endgroup\$ Commented Jan 23, 2015 at 4:12
  • 2
    \$\begingroup\$ that's cool, thanks for sharing! I do like easier questions like this because it gives more people a chance to participate and generates lots of content in the form of answers. \$\endgroup\$
    – hmatt1
    Commented Jan 23, 2015 at 4:22
  • 1
    \$\begingroup\$ Does it has to be ASCII? i.e., if a technology is not ASCII compatible, could the results reflect that? \$\endgroup\$ Commented Apr 13, 2019 at 11:13
  • 6
    \$\begingroup\$ Is it acceptable to output with a separator other than spaces (e.g. a newline)? \$\endgroup\$ Commented Sep 3, 2019 at 9:51

65 Answers 65

1 2
3
0
\$\begingroup\$

Rust, 40 bytes

|s|for c in s.bytes(){print!("{:b} ",c)}

Try it online!

An anonymous function that takes in an &str, iterates over bytes, and prints them as a binary string.

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

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

05AB1E, 2 1 byte

b

Try it online! Beats all other answers. Takes input as a list of code points.

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

Go, 116 bytes

package main
import(."fmt";"os";."io/ioutil")
func main(){b,_:=ReadAll(os.Stdin);for c:=range b{Printf("%b ",b[c])}}

Try it online!

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

Thunno 2 ṠB, 1 byte

Attempt This Online!

Or, 4 bytes flagless:

Cḃðj

Attempt This Online!

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