g | x | w | all
Bytes Lang Time Link
081AWK250307T160551Zxrs
054Tcl250302T015419Zsergiol
002Jalapeño250302T220048ZATaco
045Swift 6250302T185945ZmacOSist
031Bash +coreutils250302T105851Zroblogic
001Thunno 2 ṠB230615T172602ZThe Thon
006K ngn/k220515T074107Zoeuf
116Go220515T190157Zsech1p
045Rust220515T052945ZSapherey
030Zsh extendedglob190831T235016ZGammaFun
00105AB1E201117T192204ZMakonede
003Vyxal S210506T190631ZAaroneou
356brainfuck200903T171024ZRezNesX
076MUMPS201118T174825ZJoã
040Rust201118T161534ZAiden4
007Husk201117T082429ZDominic
006Pip s200904T034957ZRazetime
052JavaScript V8200630T222745Znph
nanx8616 machine code190415T155821Z640KB
003Shakti200222T080202Zchrispsn
044PHP200221T214423Z640KB
010Burlesque200221T184446ZDeathInc
047Elixir190903T171905ZHauleth
041Erlang190903T180853ZHauleth
004Japt S190416T100103ZShaggy
039Python 3190903T092620ZDivy
039Python 3190903T072646ZJitse
004Gaia190813T132308ZEdgex42
01005AB1E190419T071715ZElcan
016Perl 6190418T230708ZJo King
000MITS Altair 8800190418T191911Z640KB
021Perl 5190416T185415ZXcali
00405AB1E190416T155034ZKevin Cr
034><>190416T114503ZEmigna
024Ruby190415T213710ZJordan
045Forth gforth190415T132911Zreffu
071JavaScript ES6190413T004328Zthomasco
046PowerShell190413T173251ZGMills
112Java 8190413T221555ZBenjamin
101Commodore VIC20/C64/128 and TheC64Mini190413T152441ZShaun Be
008Deorst171106T212010Zcaird co
059PHP171102T051843ZJo.
039Python 3.6171101T153942Zovs
102Shell utils171101T153100Zjuh
018q/kdb+171101T144453Zmkst
023Julia171101T141819ZEricSher
012APL Dyalog171101T132959ZErik the
052Tcl171101T120622Zsergiol
003Jelly171101T123249ZErik the
055Scala 55 Bytes150124T233318ZDominik
071Erlang150123T065029Zc.P.u1
024Ruby150122T231636ZMartin E
043Python 3150123T223404Zmatsjoyc
063JavaScript ES6150122T233342ZNinjaBea
038Ruby 38 Bytes150123T222745Zkyle k
078JavaScript150123T201934Zwolfhamm
nanPostscript150123T184043ZAJMansfi
148Java 148 Bytes150123T113949ZBryan De
119C++150123T040313ZBMac
010Pyth150122T225522ZOptimize
065Haskell150123T104428Zproud ha
064Cobra150123T101442ZΟurous
nanNever will C# win these kinds of questions but here's a try150123T085514ZAbbas
068Golang150123T025008Zhmatt1
009J150123T015753Zswish
041Python 3150123T014256ZSp3000
158STATA150123T003400Zbmarks
nan150123T001158ZDavid
052Python150122T225450ZKSFT
008CJam150122T230003ZOptimize

AWK, 81 bytes

@load "ordchr";{for(;i++<NF;s=s" ")for(b=7;b;)s=s and(rshift(ord($i),--b),1)}$0=s

Attempt This Online!

Tcl, 54 bytes

binary s {*}$argv B* b;puts [regsub -all .{8} $b {& }]

Try it online!

Jalapeño, 2 bytes

Converts the string to a list bytes, then each byte to their binary representation.

Hex-Dump of Bytecode

       0  1  2  3  4  5  6  7  8  9  A  B  C  D  E  F
0000: 1e 47                                           

Try it Online!

Swift 6, 45 bytes

{($0+"").map{String($0.asciiValue!,radix:2)}}

Bash +coreutils, 31 bytes

xxd -b -c1|awk '{printf $2" "}'

Try it online!

Thunno 2 ṠB, 1 byte

Attempt This Online!

Or, 4 bytes flagless:

Cḃðj

Attempt This Online!

K (ngn/k), 12 6 bytes

{2\'x}

Try it online!

-6 bytes thanks to @coltim

Returns a 2D array.

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!

Rust, 47 45 bytes

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

Try it online!

A closure iterating over the string, converting each character to its ascii number, then printing out it's binary equivalent with :b

Turns out you don't need {} always to define a closure

Zsh --extendedglob, 30 bytes

<<<${1//(#m)?/$[[##2]#MATCH] }

Try it online!

//Replace every (#m) matched ? single character with its binary representation. [#2] leaves a 2# prefix on, [##2] strips it.


Zsh, 43 36 bytes

for c (${(s::)1})printf $[[##2]#c]\ 

Try it online! Try it online! Try it online!

for c (${(s::)1})                           # for character in string $1   (e.g. H)
                       $[       ]          # arithmetic evaluation
                       $[     #c]          # get character code           (e.g. 72)
                       $[[##2]#c]          # convert to base 2            (e.g. 1001000)
               printf  $[       ]\    # append space, print

If all binary strings need to be 8 characters long, +5 bytes:

for c (${(s::)1})printf %08d\  $[[##2]#c]

Try it online!

05AB1E, 2 1 byte

b

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

Vyxal S, 3 bytes

bƛṅ

Try it Online!

Explanation:

b    # Convert each letter to a list of bits
 ƛ   # In each list of bits:
  ṅ  #   Join elements
     # 'S' flag - join top of stack with spaces and output

brainfuck, 465 455 443 356 bytes

>>,[<+<+>>-]<[[-]>+++++++>+[>[-]>>[-]<<<<<<[>>>>+>+<<<<<-]>>>>[<<<<+>>>>-]<<[>>+>>+<<<<-]>>>>[<<<<+>>>>-]<<[>>[-]++<[->-[>+>>]>[+[-<+>]>+>>]<<<<<]>>[-]>[<<<+>>>-]<<<<-]>>[-]++<[>->+<[>]>[<+>-]<<[<]>-]<<[-]>>>>[<<<+<+>>>>-]<<<<<<[>>>+<<<-]>>>[[-]>>[-]-[<+>-----]<---[<<+<<+>>>>-]<<.[-]>]<<[>+>+<<-]>>-[<<+>>-]<]++++[>++++++++<-]>.<<<<[-]>[-]>[-],[<+<+>>-]<]

Try it online!

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

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.

Husk, 7 bytes

wm(ṁsḋc

Try it online!

Pip -s, 6 bytes

TB*A*a

Converts to ASCII/Unicode, then to binary. the -s flag joins lists on space.

Try it online!

JavaScript (V8), 52 bytes

a=>[...a].map(p=>p.charCodeAt().toString(2)).join` `

Try it online!

Beats all existing JS answers.

x86-16 machine code, IBM PC DOS, 33 31 28 bytes

Binary:

00000000: d1ee ad8a c849 b308 ac92 b000 d0e2 1430  .....I.........0
00000010: cd29 4b75 f5b0 20cd 29e2 ebc3            .)Ku.. .)...

Listing:

D1 EE       SHR  SI, 1          ; point SI to DOS PSP (80H) 
AD          LODSW               ; load input string length into AL, SI to 82H  
8A C8       MOV  CL, AL         ; set up loop counter 
49          DEC  CX             ; remove leading space/slash from char count 
        LOOP_CHAR: 
B3 08       MOV  BL, 8          ; loop 8 bits 
AC          LODSB               ; load next char 
92          XCHG AX, DX         ; use DX for bit shift 
        LOOP_BIT: 
B0 00       MOV  AL, 0          ; clear AL 
D0 E2       SHL  DL, 1          ; high-order bit into CF 
14 30       ADC  AL, '0'        ; AL = '0' + CF              
CD 29       INT  29H            ; write AL to screen 
4B          DEC  BX             ; decrement bit counter 
75 F5       JNZ  LOOP_BIT       ; loop next bit              
B0 20       MOV  AL, ' '        ; display a space 
CD 29       INT  29H            ; write AL to screen 
E2 EB       LOOP LOOP_CHAR      ; loop next char 
C3          RET                 ; return to DOS

A standalone PC DOS executable COM file. Input is via command line, output to screen.

enter image description here

Shakti, 8 3 bytes

In 2020.02.19:

10/2\`c?

Example:

 10/2\`c?"Hello World!"
1001000 1100101 1101100 1101100 1101111 100000 1010111 1101111 1110010 1101100 1100100 100001

Update: 3 bytes in 2020.02.23, via Arthur: 2\'

PHP, 44 bytes

while($c=decbin(ord($argn[$i++])))echo"$c ";

Try it online!

Burlesque, 10 bytes

XX{**b2}mw

Try it online!

XX  # Break into characters
{
 ** # Convert to ascii val
 b2 # Convert to binary
}mw # Map and spaces between elements 

Elixir 47

for c<-:io.read(''),do: :io.fwrite('~.2B ',[c])

As function:

& for<<c<-&1>>,do: :io.fwrite('~.2B ',[c])

As function working on charlists (' delimited strings):

& for c<-&1,do: :io.fwrite('~.2B ',[c])

Erlang 41

[io:fwrite('~.2B ',[C])||C<-io:read("")].

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

Or, if we can take input as an array of characters ...

Japt -mS, 2 bytes

Try it

Python 3, 39 bytes

for i in input():print(bin(ord(i))[2:])

Try it online!

Python 3, 39 bytes

print(*[f'{ord(i):b}'for i in input()])

Try it online!

Slight improvement over the current leading Python answer

Gaia, 4 bytes

ċb¦ṡ

Pretty happy with this

How it works

ċ   Get a list of all the code points in the input string
b¦  Convert every number in that list to binary
ṡ   Joins the element of the list with spaces
Implicit Output 

Try it Online!

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!

Perl 6, 16 bytes

*.ords>>.base(2)

Try it online!

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

MITS Altair 8800, 0 bytes

Input string is at memory address #0000H (allowed). Output in binary via front panel I/O lights D7-D0.

Example, do RESET, then EXAMINE to see the first byte, followed by repeating EXAMINE NEXT to see the rest.

"H" = 01 001 000:

enter image description here

"e" = 01 100 101:

enter image description here

"l" = 01 101 100:

enter image description here

Try it online!

Non-competing, of course. :)

Perl 5, 21 bytes

printf'%b ',ord for@F

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.

><>, 34 bytes

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

Try it online!

Ruby, 24 bytes

I was hoping to beat Martin Ender, but I only managed to tie him.

Takes input on STDIN; +1 bytes for -p flag.

gsub(/./){"%b "%$&.ord}

Try it online!

Forth (gforth), 45 bytes

: f 0 do dup i + c@ 2 base ! . decimal loop ;

Try it online!

Code Explanation

: f           \ start a new word definition
  0 do        \ start a loop from 0 to string length - 1
    dup i +   \ duplicate the address and add the loop index to get addr of current character
    c@        \ get the character at that address
    2 base !  \ set the base to binary
    .         \ print the ascii value (in binary)
    decimal   \ set the base back to decimal
  loop        \ end the loop
;             \ end the word definition

JavaScript ES6, 71 bytes

alert(prompt().split('').map(c=>c.charCodeAt(0).toString(2))).join(' ')

PowerShell, 63 59 46 bytes

-13 bytes thanks to @mazzy

"$($args|% t*y|%{[Convert]::ToString(+$_,2)})"

Try it online!

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!

Commodore VIC-20/C64/128 and TheC64Mini, 101 tokenized BASIC bytes

Here is the obfuscated listing using Commodore BASIC keyword abbreviations:

 0dEfnb(x)=sG(xaNb):inputa$:fOi=1tolen(a$):b=64:c$=mI(a$,i,1):fOj=0to6
 1?rI(str$(fnb(aS(c$))),1);:b=b/2:nEj:?" ";:nE

Here for explanation purposes is the non-obfuscated symbolic listing:

 0 def fn b(x)=sgn(x and b)
 1 input a$
 2 for i=1 to len(a$)
 3 let b=64
 4 let c$=mid$(a$,i,1)
 5 for j=0 to 6
 6 print right$(str$(fn b(asc(c$))),1);
 7 let b=b/2
 8 next j
 9 print " ";
10 next i

The function fn b declared on line zero accepts a numeric parameter of x which is ANDed with the value of b; SGN is then used to convert x and b to 1 or 0.

Line one accept a string input to the variable a$, and the loop starts (denoted with i) to the length of that input. b represents each bit from the 6th to 0th bit. c$ takes each character of the string at position i.

line 5 starts the loop to test each bit position; right$ is used in line 6 to remove a auto-formatting issue when Commodore BASIC displays a number, converting the output of fn b to a string; asc(c$) converts the current character to its ascii code as a decimal value.

Line 7 represents the next bit value. The loop j is ended before printing a space, then the last loop i is ended.

C64 converting PETSCII to Binary

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']

PHP, 59 bytes

<?foreach(str_split($argv[1])as$a)echo decbin(ord($a)).' ';

Try it online!

Python 3.6, 39 bytes

print(*[f'{ord(c):b}'for c in input()])

Try it online!

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]

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 " "

Julia, 23 bytes

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

APL (Dyalog), 12 bytes

10⊥2⊥⍣¯1⎕UCS

Try it online!

Thanks to Adám for help with this solution.

Tcl, 52 bytes

proc B s {binary s $s B* b;regsub -all .{8} $b {& }}

Try it online!

Jelly, 3 bytes

OBK

Try it online!

Scala - 59 - 55 Bytes

readLine().map(x=>print(x.toByte.toInt.toBinaryString))

Normally, one should use foreach and not map.

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]).

Ruby, 34 28 24 bytes

$<.bytes{|c|$><<"%b "%c}

Takes input via STDIN. 6 bytes saved thanks for AShelly and another 4 thanks to britishtea.

Python 3 - 43 bytes


print(*map("{:b}".format,input().encode()))

No quite the shortest, but an interesting approach IMO. It has the added advantage that if the number of significant bits varies, E.G. Hi!, padding with zeros is trivial (2 more bytes, as opposed to 9 for .zfill(8)):

print(*map("{:08b}".format,input().encode()))

JavaScript ES6, 63 65 bytes

alert([for(c of prompt())c.charCodeAt().toString(2)].join(' '))

This is rather long, thanks to JavaScript's long function names. The Stack Snippet below is the rough ES5 equivalent, so it can be run in any browser. Thanks to edc65 for the golfing improvements.

alert(prompt().split('').map(function(e){return e.charCodeAt().toString(2)}).join(' '))

Ruby 38 Bytes

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

JavaScript 78

alert(eval('""'+prompt().replace(/./g,"+' '+'$&'.charCodeAt().toString(2)")))

Postscript, 17 bytes (13 byte program + 4 character command line switch)

s { 2 7 string cvrs = } forall

Which is 31 bytes in tokenized form: (backticks denote literal characters, everything else is hexidecimal)

`s{2 7` 92A5 9230 `=}` 9249

Run using Ghostscript as:

gs -ss="Hello World!" string-to-binary.ps

Java - 148 Bytes

public class sToB{public static void main(String[] a){for(int i=0;i<a[0].length();i++){System.out.print(Integer.toString(a[0].charAt(i) ,2)+" ");}}}

Edited to include full file

C++ - 119 bytes

Freeing memory? What's that?

#include<cstdio>
#include<cstdlib>
int main(int c,char**v){for(*v=new char[9];c=*(v[1]++);printf("%s ",itoa(c,*v,2)));}

(MSVC compiles the code with warning)

Pyth, 10 bytes

jdmjkjCd2z

Python mapping and explanation:

j           # join(                               "Join by space"
 d          #      d,                             "this space"
 m          #      Pmap(lambda d:                 "Map characters of input string"
  j         #                    join(            "Join by empty string"
   k        #                        k,           "this empty string"
    j       #                        join(        "This is not a join, but a base conversion"
     C      #                             Pchr(   "Convert the character to ASCII"
      d     #                                  d  "this character"
            #                                 ),
     2      #                             2       "Convert to base 2"
            #                            )
            #                        ),
  z         #           z)))                      "mapping over the input string"

Input is the string that needs to be converted without the quotes.

Try it here

Haskell, 65

m s=tail$do c<-s;' ':do j<-[6,5..0];show$mod(fromEnum c`div`2^j)2

heavy use of the list monad. it couldn't be converted to list comprehentions because the last statements weren't a return.

Cobra - 64

As a Lambda:

do(s='')=(for c as int in s get Convert.toString(c,2)).join(' ')

Never will C# win these kinds of questions but here's a try, completely without encoding. :)

C# - 84

Console.Write(String.Join(" ",Console.ReadLine().Select(x=>Convert.ToString(x,2))));

Golang - 68

I'm new to Go and I'm not sure on the rules for counting characters in this language on here either.

Imports (11 bytes):

import"fmt"

Function (55 bytes):

func c(s string){for _,r:=range s{fmt.Printf("%b ",r)}}

You can run it here.

J - 9

1":#:3&u:

No idea how to make this in a row without doubling the length of the code, I need J gurus to tell me :)

1":#:3&u:'Hello world!'
1001000
1100101
1101100
1101100
1101111
0100000
1110111
1101111
1110010
1101100
1100100
0100001

Python 3, 41 bytes

print(*[bin(ord(x))[2:]for x in input()])

Like KSFT's answer, but I thought I'd point out that, in addition to raw_input -> input, Python 3 also has an advantage here due to the splat for print.

STATA 158

I used a different approach than most. Read in via the display _request() prompt (shortened to di _r(r)). Write the string to a file called b in text mode. Open b in binary mode and read each character as a byte and convert to binary. Technically the b file should be closed at the end, but it is a valid program and runs successfully without it.

di _r(r)
file open a using "b",w
file w a "$r"
file close a
file open a using "b",b r
file r a %1bu t
while r(eof)==0 {
loc q=t
inbase 2 `q'
file r a %1bu t
}

Matlab

This is an anonymous function

f=@(x) dec2bin(char(x))

usage is f('Hello World').

Alternatively, if x is defined as the string Hello World! in the workspace, then just dec2bin(char(x)) will work.

Python - 52

print" ".join([bin(ord(i))[2:]for i in raw_input()])

I'll work on translating this into Pyth. Someone else did a Pyth answer already.

If you don't care about it being a full program or about I/O format and use Python 3, you can do it in 23 bytes like this:

[bin(ord(i))for i in x]

x is the input.

I know this doesn't count because the interpreter wasn't released before the challenge was posted, but here it is in KSFTgolf:

oan

CJam, 8 bytes

l:i2fbS*

Easy-peasy:

l:i           "Read the input line and convert each character to its ASCII value";
   2fb        "Put 2 on stack and use that to convert each ASCII value to base 2";
      S*      "Join the binary numbers by space";

Try it here