# String to Binary

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.

• We had the reversed asked: codegolf.stackexchange.com/questions/35096/… – chilemagic Jan 23 '15 at 0:57
• 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. – ericmarkmartin Jan 23 '15 at 4:12
• 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. – chilemagic Jan 23 '15 at 4:22
• Can I take input via a file (as specified at meta.codegolf.stackexchange.com/a/2452/30076)? – bmarks Jan 23 '15 at 16:08
• Does it has to be ASCII? i.e., if a technology is not ASCII compatible, could the results reflect that? – Shaun Bebbers Apr 13 at 11:13

# Ruby 38 Bytes

p gets.bytes.map{|i|i.to_s 2}.join ' '

• .join ' ' can be shortened to *' '. – Martin Ender Jan 23 '15 at 22:59

# Erlang, 71 Bytes

f(S)->L=lists,L:droplast(L:flatten([integer_to_list(X,2)++" "||X<-S])).


If a trailing whitespace at the end is allowed then

# 55 Bytes

f(S)->lists:flatten([integer_to_list(X,2)++" "||X<-S]).


# Julia, 23 bytes

f(n)=[bin(a)for a in n]


# q/kdb+, 18 bytes

Solution:

" "sv"01"0b vs'4h$ Example: q)" "sv"01"0b vs'4h$"Hello World!"
"01001000 01100101 01101100 01101100 01101111 00100000 01010111 01101111 01110010 01101100 01100100 00100001"


Explanation:

Convert string to byte array, convert each byte to binary, index into the string "01" and then join strings together with whitespace " ":

" "sv"01"0b vs'4h$/ the solution 4h$ / cast ($) to type byte (4h) 0b vs' / convert each (') byte to binary (0b vs) "01" / (implicit) index into "01" (false;true) " "sv / join (sv) with " "  # Shell utils, 102 bytes I'm kind of ashamed of this. xxd -b|sed -r "s/^.*: //;s/ .+$//;s/0*([01]+)/\1/g"|tr \n\r " "|sed -r "s/ +/ /g;s/ *1101 *1010 *$//"  Since I'm (possibly) running utilities from various archives across the Internet on Windows, I'll list the version information for each: xxd V1.10 27oct98 by Juergen Weigert GNU sed version 4.2.1 tr (GNU textutils) 2.0 Microsoft Windows [Version 10.0.15063]  # PHP, 59 bytes <?foreach(str_split($argv[1])as$a)echo decbin(ord($a)).' ';


Try it online!

# Deorst, 8 bytes

vombkE]_


Try it online!

## How it works

Example input: ab

vo       - Map ordinal;      STACK = [[97, 98]]
mb     - Map binary;       STACK = [['1100001', '1100010']]
k    - Turn off sort;    STACK = [['1100001', '1100010']]
E]  - Flatten;          STACK = ['110001', '1100010']
_ - Join with spaces; STACK = ['110001 1100010']


# Java 8, 112 bytes

Takes input as a command line argument

interface M{static void main(String[]a){a[0].chars().forEach(i->System.out.print(Long.toBinaryString(i)+" "));}}


Try it online!

• I'm so tired right now that I forgot I already did this challenge and almost posted a duplicate. – Benjamin Urquhart Apr 16 at 12:32

# Japt-S, 4 bytes

¬®c¤


Try it

¬®c¤     :Implicit input input of string U
¬        :Split
®       :Map
c      :  Character code
¤     :  To binary string
:Implicitly join with spaces and output


# ><>, 34 bytes

i\~48*o
?\:0(?;:2%:}-2,:0=
?\{nl1=


Try it online!

# 05AB1E, 4 bytes

Çbðý


Try it online.

Explanation:

Ç     # Convert the (implicit) input-string to a list of unicode values
b    # Convert each integer to a binary string
ðý  # Join by spaces (and output the result implicitly)


Could be just the first two bytes if a list output is allowed, or 3 bytes with » if a newline delimiter instead of space delimiter is allowed.

# Perl 5, 21 bytes

printf'%b ',ord for@F


Try it online!

# Perl 6, 16 bytes

*.ords>>.base(2)


Try it online!

Converts the string to codepoints and then each to base 2.

# 05AB1E, 10 bytes

SvyÇ2Bнð«?

S           //Split string
v          //Loop on every character
y         //Push current character to the stack
Ç        //Get ASCII value of character
2B      //Convert to base 2
н     //Convert from 1-element array to string
ð«   //Append space
?  //Print with no newline


Try it online!