# Setting the Scene:

It is an average afternoon, as you unwind after a long day by engaging in your favorite pastime: answering the plethora of captivating questions on PPCG. It hasn't been a good day for you; those damned Pyth programmers keep getting best answer and it's killing your vibe. Suddenly, you notice a new question has been posted! You make up your mind; not this time CJammers. Furiously, you begin to type, birthing a stream of characters; a fetus of the glorious BrainFuck loop that your end product will be. You type and type. Faster! Faster still! You're so focused you don't even notice the lights on your screen start to flicker on and off. Suddenly, the monitor glows green, an alien like symbol branded upon the desktop. Using your impeccable skills you cat ~/Desktop/aliensymbol and get presented with a bunch of numbers, a message! Being the shrewd PPCG programmer that you are, you notice that it looks like ASCII. No matter for you, you print(chr(i) for i in file) and decipher the message. Your first thought: "I need to share this with the community!".

...

Who would've thought it? That the first people to make contact with aliens would be the humble programmers of PPCG. Why us? Maybe it is because we are the epitome of human intelligence. Or maybe because BrainFuck is about as close as we have to an alien language. Nevertheless, the aliens - being the ultra-intelligent life forms that they are - want to test whether they should classify the human race as intelligent or not. As a test of our mental prowess, the aliens have asked us to send them a few computer programs to demonstrate that we are technologically cultured. The issue is, the only human language they understand is numerical ASCII!

Help humankind show those creatures who the real intellectual alpha is. We need to send them a script that will convert our text based source codes into their numerical ASCII versions. Unfortunately, due to our underdeveloped technology (thanks Obama), we must send as small a translating program as possible. Good thing they chose to contact PPCG!

# The Challenge:

The premise of this challenge is simple: you are to write a program that will take any program's source code (or any general text file for that matter), and output a space separated version of it with an ASCII translation below each line. That is, given an input of a text file, you need to output each line of that file followed by a line containing the ASCII representation of each character in the line above it (with each number lining up with the character it represents).

An example will greatly clarify this. Taking the source code of the infamous hello world as the input:

#include <stdio.h>

int main() {
printf("hello, world\n");
return 0;
}


#   i   n   c   l   u   d   e       <   s   t   d   i   o   .   h   >
35  105 110 99  108 117 100 101 32  60  115 116 100 105 111 46  104 62  10

10
i   n   t       m   a   i   n   (   )       {
105 110 116 32  109 97  105 110 40  41  32  123 10
p   r   i   n   t   f   (   "   h   e   l   l   o   ,       w   o   r   l   d   \   n   "   )   ;
32  32  32  32  112 114 105 110 116 102 40  34  104 101 108 108 111 44  32  119 111 114 108 100 92  110 34  41  59  10
r   e   t   u   r   n       0   ;
32  32  32  32  114 101 116 117 114 110 32  48  59  10
}
125 10


# Implementation Details:

You may choose to take the input in whatever form you wish (opening file, command line pipe, function parameter etc.) and you should output to stdout or return the output from a function.

Things to note

• Each character in the output is separated by a '\t' to allow for the space of 3 digits in the line below to line up (we are going to assume your tab is set to 4 spaces or more).
• The newlines are displayed on the line that the return key was pressed (notice the 10's in the example)
• The '\t' and '\n' printing requirements are lax. Your output must, in the general sense, look pleasing to the eye (need to show those aliens we have aesthetic sense as well), and if you can bend the previous two points while maintaining visual integrity, you will be forgiven.
• You can choose to assume whether or not there is a newline at the end of the last line.

# Scoring

This is code-golf so shortest program wins. Note, even if your program isn't the shortest, but uses really neat tricks relative to the language you are using, many +1's to you!

Good luck. The intergalactic intelligence ranking of humanity rests on your shoulders.

Note: Please forgive the plot holes. I'm not a writer :D

• Maybe drop or greatly simplify the "plot" and just leave the challenge. I'm honestly not going to read 3 paragraphs of backstory. I'm just going to read the challenge, since that's what I'm here for. – mbomb007 Sep 2 '16 at 20:26
• @YetiCGN The line break is acting as a line break (you can't see the character representing it but you can see the break) and you still need to explicitly write it in the ascii version. Think of it as if you regex searched the source code for '\n'. Everywhere your search would highlight is where you should write the 10. Here is an example from vim (ignore the last one). – gowrath Sep 2 '16 at 20:32
• @mbomb007 tbh, that was a pretty epic backstory. I kinda enjoyed it – Maltysen Sep 2 '16 at 20:32
• @mbomb007 Those who want to read can, those who don't can go straight to the challenge part; such is the beauty of subtitles. – gowrath Sep 2 '16 at 20:34
• I read it as "the first people to make contact with humans would be the humble programmers of PPCG." – marczellm Sep 2 '16 at 23:13

# Dyalog APL, 14 bytes

Takes list of strings that include newline sequences (10 or 13 10 etc.)

↑(⊢,[.5]⎕UCS)¨


↑ matrify the list of lists consisting of

(...)¨ for each line return...

⊢ the text itself

,[.5] followed in a new dimension before the first dimension by

⎕UCS the Unicode Character Set code points

TryAPL online!

• Aren't you calling this once per each line rather than taking in all the input at once? – Steven H. Sep 5 '16 at 3:57
• @StevenH. No, the (right-most part of the) function contains a loop, but as a whole, it is called just once. See the TryAPL example that f is called directly on the input. To call a function once per line, if would have said f¨. – Adám Sep 5 '16 at 5:45

# Pyth, 17 13 bytes

Another one of those Pyth programmers. Sorry.

The new code requires each line to be wrapped in quotes and escaped (including newlines if you desire them to be printed), but puts an extra newline between the line and the ASCII.

jjLC9smCMBd.Q


Try it online!

Explanation:

           .Q  Evaluate all input lines
m        For each of those lines:
Bd     Return the line and
CM       the line mapped to ASCII characters
s         Sum all these together to begin alternating between line and mapped line
jLC9          Join all the characters and numbers in the lines and mapped lines on tabs
j              And join all of those on newlines


I'm keeping the old code and its' explanation below.

#Jw
jKC9J
jK+CMJT


Try it online! or use an easier-to-read test case.

Explanation:

#                  Until we run into an error:
Jw                 Read in the next line of input and call it J.
(When there is no line of input, error is thrown that ends program.)

j                  Join:
J               all characters in input line
KC9                and insert tab characters (C9), which we can refer to later as K.
(Setting it to a variable doesn't save or lose bytes.)

Implicit print that join with trailing newline.

j                  Join:
CMJ              the mapping of ASCII numbers to characters in the input,
K                  inserting tab characters in between every number
+   T             And with a trailing 10 for the newline at the end.

• I think your first link doesn't work as well as the second one does (doesn't print new lines I think). You might want to update the code in it. – gowrath Sep 3 '16 at 6:45

# Python 2, 105 bytes

This uses a slightly different approach than the OP's answer. Note that SO messes up my literal tabs with spaces.

def f(s):
o=x=''
for c in s:
o+=c+"  ";x+="%s    "%ord(c)
if"\n"==c:print o[:-1],x;o=x=''
print o+"\n"+x


Try it online

• Wouldn't x+=ord(c)+" " be shorter than "%s "%ord(c)? – DJMcMayhem Sep 2 '16 at 21:30
• @DJMcMayhem You cannot append an integer to a string. x+=ord(c)+" " is the same length. – mbomb007 Sep 2 '16 at 21:32

# Vim, 86, 77 keystrokes

:g/^/norm A<C-v><C-v>10<C-v><esc>Yp:s/./\=char2nr(submatch(0))."\t"/g<C-v><cr>k:s/./&\t/g<C-v><cr>
:%s/<C-v><cr>0<cr>


This is way too long, but that's what you get when you use vim's eval feature (\=).

• Can't tell if your \= is explaining the eval feature, or a sad face because you're using the eval feature ... – AdmBorkBork Sep 2 '16 at 20:52
• @timmyD The first. This is my sad face. D; – DJMcMayhem Sep 2 '16 at 20:52
• – Downgoat Sep 3 '16 at 0:37

# Perl, >33 31 bytes

Includes +3 for -F (cannot be combined with -e and the code has ' too, so space and - are counted too).

Run with the input on STDIN or give one or more filenames as argument

perl -M5.010 asciidump.pl <<< "Hoi"


asciidump.pl

#!/usr/bin/perl -F
$"=v9;say"@F@{[unpack'W*']}"  Output is not aesthetic if the text contains a tab or there is no final newline # Pyth, 21 bytes j.imjC9d.zmjC9+CMdT.z  Here's an entry from one of "those damned Pyth programmers" ;) A program that takes input of an unquoted multiline string on STDIN and prints the result. This assumes that all input has a trailing newline. If you want it to deal with no trailing newline, add < at the beginning of the program and )_3 at the end. Try it online The output for the test case is difficult to see in the online interpreter, so I've included it here: # i n c l u d e < s t d i o . h > 35 105 110 99 108 117 100 101 32 60 115 116 100 105 111 46 104 62 10 10 i n t m a i n t ( ) { 105 110 116 32 109 97 105 110 116 40 41 32 123 10 p r i n t f ( " h e l l o , w o r l d \ n " ) ; 32 32 32 32 112 114 105 110 116 102 40 34 104 101 108 108 111 44 32 119 111 114 108 100 92 110 34 41 59 10 r e t u r n 0 ; 32 32 32 32 114 101 116 117 114 110 32 48 59 10 } 125 10  How it works j.imjC9d.zmjC9+CMdT.z Program. Input: .z (All of STDIN split on newlines) C9 Yield tab character by taking chr(9) j Join on tab m d.z Map that over .z CM Map ord() + T Append 10 jC9 Join on tab m d .z Map that over .z .i Interleave the results of the two mappings j Join on newlines Implicitly print  • you can save 2 bytes by getting rid of the .z and taking input as list of lines – Maltysen Sep 2 '16 at 22:40 • @Maltysen Judging by the question and the other answers, I think splitting on newlines is part of the challenge. – TheBikingViking Sep 2 '16 at 22:55 ## C, 136117 114 Bytes #define p(s)while(l[i])printf(s,l[i++]);i=0;puts("") char l[99];i;f(f){while(fgets(l,99,f)){p("%c\t");p("%d\t");}}  char l[99];i;f(f){while(fgets(l,256,f)){i=0;while(l[i])printf("%c\t",l[i++]);i=0;puts("");while(l[i])printf("%d\t",l[i++]);puts("");}}  Can be tested like this infile; main(c,v)char**v; { infile=fopen(v[1],"r"); f(infile); }  • Having char**v inside the main() would save you 2 characters there.. – Alexis Wilke Sep 2 '16 at 21:59 • Uh, I only count 114 bytes in that code. I think you're using CRLF instead of LF (117->115), plus you have a trailing newline (115->114). – Tim Čas Sep 3 '16 at 11:58 ## PowerShell v2+, 85 bytes gc$args|%{$a=$b='';$_-split'(.)'-ne''|%{$a+="$_t";$b+="$(+[char]$_)t"};$a;$b+"10"}


Input is via a path to a file. We then Get-Content (alias gc) on that file, which automatically splits on newlines. We loop over each of those |%{...}. Start by setting $a and $b to an empty string -- these are our ASCII characters and code points respectively. We then -split the input line on every character, keeping it (.), and removing the empty values -ne'' (it's due to how .NET regex parses), then send those into another loop.

Each inner loop, we string concatenate the current character with a tab t and add that onto $a. Similarly for $b, excepting we're explicitly casting as a char and then as an int +.

Outside the inner loop, we place the resulting $a and $b (with a linefeed designator, since that'll never come up in our looping) on the pipeline. Those are gathered up with an implicit Write-Output at program completion.

### Example

(with the understanding that I've got a trailing linefeed and Markdown mangles the tab character)

PS C:\Tools\Scripts\golfing> .\aliens-only-understand-ascii.ps1 '.\aliens-only-understand-ascii.txt'
#   i   n   c   l   u   d   e       <   s   t   d   i   o   .   h   >
35  105 110 99  108 117 100 101 32  60  115 116 100 105 111 46  104 62  10

10
i   n   t       m   a   i   n   t   (   )       {
105 110 116 32  109 97  105 110 116 40  41  32  123 10
p   r   i   n   t   f   (   "   h   e   l   l   o   w   ,       w   o   r   l   d   \   n   "   )   ;
32  32  32  32  112 114 105 110 116 102 40  34  104 101 108 108 111 119 44  32  119 111 114 108 100 92  110 34  41  59  10
r   e   t   u   r   n       0   ;
32  32  32  32  114 101 116 117 114 110 32  48  59  10
}
125 10

• Damn, I just wrote a PowerShell answer while somehow managing to completely miss this one! Always glad to see posh represented though! :) – briantist Sep 4 '16 at 4:33

# ><> (Fish), 48 Bytes

>i:0(?v::oa=?v9o
2';'oa<.20r  <~p3
o9nv?=0l
voa<


A task the language shines at! I'm sure I could probably have golfed out a little bit more, but it's been a long week. Might take a look at it later though.

Try it online!

Alternate try it online that looks nicer in my opinion, but the text wrapping messes up a bit for input with long lines like the test case.

# PHP, 131 115 bytes

Just a FGITW, maybe I can improve upon it more. Turns out, I can!

First version at 131 bytes:

<?$s=file($argv[1]);foreach($s as$l){$a=str_split(trim($l));echo join(' ',$a).' ';$i=0;while($c=ord($l{$i++}))echo$c.'  ';echo'
';};


The filename is supplied as the first argument after the script name: php aliens.php helloworld.c

Second version at 115 bytes:

function($s){foreach($s as$l){$a=str_split($l);echo join(' ',$a);$i=0;while($c=ord($a[$i++]))echo$c.' ';echo' ';}};  The first version accounts for missing newlines at the end of the line, but after the additional clarification, we can leave that code out and put everything in a function to save 16 bytes. The whitespace characters in join(' ',$a) as well as in echo$c.' ' are tab characters = \t. The newlines in the code are on purpose. # Python 3, 89 77 bytes more golfed version based on the same idea: def f(s): for a in s:print(*map('{:>4}'.format,[*a,*a.encode()]),end='\n\n')  If there are '\t's in the input, then change the 4 to a 9. ### Prior version: def f(s): for l in s: print(*l,sep='\t',end='');print(*l.encode(),sep='\t',end='\n\n')  Takes a list of strings, each ending with a '\n'. • protip: you can leave the print statements on the end of the for l in s i.e for l in s:print(*l,sep='\t',end='');print(*l.encode(),sep='\t',end='\n\n') – Destructible Lemon Sep 3 '16 at 5:15 • I haven't tested it, but I think to could take 8 off with lambda s:'\n\n'.join(*map('{:>4}'.format,[*a,*a.encode()])for a in s) – DJMcMayhem Sep 3 '16 at 14:56 # Powershell, 56 Bytes gc$args|%{($a=[char[]]$_)-join"    ";[int[]]$a+10-join" "}  Script accepts a filepath as input. Get-Content automatically splits input into an array of strings split on newlines in the source. From there I enter a foreach, cast to an array of characters and set that to$a, join that with tabs and print it. Next but still within the foreach I cast the character array to an integer array, append a linefeed and join again with a tab.

Call looks like this:

PS C:\PretendFolder> .\aoua.ps1 ".\aoua.txt"


and here is an output sample

#   i   n   c   l   u   d   e       <   s   t   d   i   o   .   h   >
35  105 110 99  108 117 100 101 32  60  115 116 100 105 111 46  104 62  10

10
i   n   t       m   a   i   n   (   )       {
105 110 116 32  109 97  105 110 40  41  32  123 10
p   r   i   n   t   f   (   "   h   e   l   l   o   ,       w   o   r   l   d   \   n   "   )   ;
32  32  32  32  112 114 105 110 116 102 40  34  104 101 108 108 111 44  32  119 111 114 108 100 92  110 34  41  59  10
r   e   t   u   r   n       0   ;
32  32  32  32  114 101 116 117 114 110 32  48  59  10
}
125 10


## JavaScript (ES6), 94 bytes

s=>s.replace(/(.*)\n?/g,(s,n)=>n&&[...s].join␉+
${[...n].map(c=>c.charCodeAt()).join␉} )  Where ␉ represents the literal tab character. Works as long as the input doesn't contain tab characters. 81-byte version that also requires the input to have a trailing newline: s=>s.replace(/.*\n/g,s=>[...s].join␉+[...s].map(c=>c.charCodeAt()).join␉+ )  • I think .split().map() may win over .replace(). Once escaped characters optimized with backticks, this should be 85 bytes: s=>s.split('\n').map(c=>(s=[...c]).join('\t')+'\n'+s.map(c=>c.charCodeAt()).join('\t')).join('\n') (Sorry, I've no idea if/how backticks can be included in a comment.) – Arnauld Sep 3 '16 at 2:30 • @Arnauld Doesn't work for (e.g.) a bare newline character. Also, to include backticks in a comment, precede them with a backslash. – Neil Sep 3 '16 at 10:07 # C#, 64 63 bytes s=>{foreach(int c in s+(s=""))s+=c+(10==c?"\n":" ");return s;};  -1 byte by using actual tab character instead of \t. Note: renders as 4 spaces above and 2 spaces in the ungolfed version below. Ungolfed: /*Func<string, string> Lambda =*/ s => { // Enumerate the ascii (int) values of the chars in s, // but at the same time set input s to "" to use at the output var. // +(s="") is 2 bytes less than var o=""; foreach (int c in s + (s="")) // Append the string representation of the ascii value, // Append new line if c was new line, otherwise append tab. s += c + (10==c ? "\n" : " "); return s; };  Results: (Note: Visual Studio has used \r\n for the new lines when I pasted the input in, hence the 13 10 line endings in output) Input: #include <stdio.h> int maint() { printf("hello, world\n"); return 0; } Output: 35 105 110 99 108 117 100 101 32 60 115 116 100 105 111 46 104 62 13 10 13 10 105 110 116 32 109 97 105 110 116 40 41 32 123 13 10 32 32 32 32 112 114 105 110 116 102 40 34 104 101 108 108 111 44 32 119 111 114 108 100 92 110 34 41 59 13 10 32 32 32 32 114 101 116 117 114 110 32 48 59 13 10 125  # CJam, 27 bytes qN/{_1/9c*N+\{is9c}%+}%AN+*  Try it online! Explanation q e# Take the whole input N/ e# Split on newlines { e# Map this block to every line: _ e# Duplicate 1/ e# Split into characters 9c* e# Join with tabs (ASCII code 9) N+ e# Append a newline \ e# Swap top elements {is9c}% e# Map each character to its ASCII code plus a tab character + e# Concatenate the two lines }% e# (end of block) AN+* e# Join with "10" plus a newline  • The 1/ and the s are unnecessary. – Martin Ender Sep 3 '16 at 8:44 # PowerShell, 61 59 bytes gc m|%{$b="$_ "[0..$_.Length]
$b-join' ' [int[]]$b-join' '}


Things to note:

• Reads the input from a file named m in the current directory.
• All line breaks, (including the one embedded in the string after the first $_) are literal linefeeds only (0xA), so one byte each. • The contents of the strings directly after the -join operators are each a single tab character (regardless of how it's rendered here). ### Ungolfed Get-Content m | ForEach-Object -Process { # Read the file line by line$b = "$_n"[0..$_.Length]   # Line by line processing strips line endings, so add a newline
# Index operator on a [string] returns a [char], or given a [range], a [char[]]
# Using $_.Length automatically accounts for the extra linebreak (0 based index)$b -join "t"               # PowerShell displays [char] as the printable character by default, so join the array with a tab
[int[]]\$b -join "t"        # We need to convert the [char]s to an [int]s so we can display it as a number
# So cast the [char[]] as [int[]], then join with tab again

}

• Your code is printing ascii 13, newline instead of 10, carriage return as in the challenge's output example – Chirishman Sep 6 '16 at 19:16
• @Chirishman PowerShell does support using only ASCII 10 for linebreaks in .ps1 files, but it can be tricky to do so in Windows with most text editors. Using SCiTE or Notepad++ or other line-ending aware editors you can ensure it's using unix endings. Once you're certain the file doesn't contain any 13's, it will print the 10's. – briantist Sep 6 '16 at 19:48

# Java, 202 bytes

s->{for(String g : s.split("\n")){g.chars().forEach(c->System.out.print((char)c+"   "));System.out.println();g.chars().forEach(c->System.out.print(c+(c<10?"  ":c<100?"  ":" ")));System.out.println();}};


I can't even be bothered making this shorter..

f q=unlines[(x>>=(++"\t").show.ord)++'\n':intersperse '\t'x|x<-lines q]


# Python 3, 92 97 107 bytes

for i in list(open(input()).read().split("\n")): for j in i: print(i+"\n"+ord(j))

p=print
s=t=''
for j in i:
r=str(ord(j))
s+=j+' '*len(r)
t+=r+' '
p(s)
p(t)


This is untested, but should work.

Thanks @gowrath for pointing out that the code isn't doing as the question specifies.

• Why not? All the input() command is doing there is getting the name of the file to open. – sonrad10 Sep 4 '16 at 21:44
• @gowrath oh, ok. – sonrad10 Sep 4 '16 at 21:46
• This doesn't overlay the text and ascii outputs as in the question spec; just prints the numbers. – gowrath Sep 4 '16 at 21:47
• @gowrath thanks, I'll fix that asap. – sonrad10 Sep 4 '16 at 21:55
• @sonrad10 Delete until fixed. – mbomb007 Sep 6 '16 at 15:06