Inspired by this SciFi.SE question.
Background (with minor spoiler):
In the movie The Martian, protagonist Mark Watney uses an ASCII table to look up hexadecimal values of ASCII characters so he can attempt to communicate back to Earth.*
Challenge
With no input, output the following ASCII table exactly like this:
Dec Hex Dec Hex Dec Hex Dec Hex Dec Hex Dec Hex Dec Hex Dec Hex
0 00 NUL 16 10 DLE 32 20 48 30 0 64 40 @ 80 50 P 96 60 ` 112 70 p
1 01 SOH 17 11 DC1 33 21 ! 49 31 1 65 41 A 81 51 Q 97 61 a 113 71 q
2 02 STX 18 12 DC2 34 22 " 50 32 2 66 42 B 82 52 R 98 62 b 114 72 r
3 03 ETX 19 13 DC3 35 23 # 51 33 3 67 43 C 83 53 S 99 63 c 115 73 s
4 04 EOT 20 14 DC4 36 24 $ 52 34 4 68 44 D 84 54 T 100 64 d 116 74 t
5 05 ENQ 21 15 NAK 37 25 % 53 35 5 69 45 E 85 55 U 101 65 e 117 75 u
6 06 ACK 22 16 SYN 38 26 & 54 36 6 70 46 F 86 56 V 102 66 f 118 76 v
7 07 BEL 23 17 ETB 39 27 ' 55 37 7 71 47 G 87 57 W 103 67 g 119 77 w
8 08 BS 24 18 CAN 40 28 ( 56 38 8 72 48 H 88 58 X 104 68 h 120 78 x
9 09 HT 25 19 EM 41 29 ) 57 39 9 73 49 I 89 59 Y 105 69 i 121 79 y
10 0A LF 26 1A SUB 42 2A * 58 3A : 74 4A J 90 5A Z 106 6A j 122 7A z
11 0B VT 27 1B ESC 43 2B + 59 3B ; 75 4B K 91 5B [ 107 6B k 123 7B {
12 0C FF 28 1C FS 44 2C , 60 3C < 76 4C L 92 5C \ 108 6C l 124 7C |
13 0D CR 29 1D GS 45 2D - 61 3D = 77 4D M 93 5D ] 109 6D m 125 7D }
14 0E SO 30 1E RS 46 2E . 62 3E > 78 4E N 94 5E ^ 110 6E n 126 7E ~
15 0F SI 31 1F US 47 2F / 63 3F ? 79 4F O 95 5F _ 111 6F o 127 7F DEL
The final newline is optional. With the newline, the md5 of the output is 58824a1dd7264c0410eb4d727aec54e1
. Without, it is 41b6ecde6a3a1324be4836871d8354fe
.
In case it helps, this is the output from the ascii
Linux command with the usage info at the top chopped off. You can recreate this on Ubuntu as follows:
sudo apt-get install ascii
ascii | tail -n+7
You may not use the ascii
utility (or similar) in your answers.
(削除) Because ASCII characters are small (削除ここまで) Enough with this silly meme already!
I am aware this is similar to Print the ASCII table, but I believe the formatting of the output in this question is significantly more complex and warrants a different question.
*Note, I have not yet seen this movie.
-
1\$\begingroup\$ So the program can be hard-coded?? \$\endgroup\$TanMath– TanMath2016年01月05日 19:58:32 +00:00Commented Jan 5, 2016 at 19:58
-
4\$\begingroup\$ +1 for the silly meme ... but why another challenge with no input? \$\endgroup\$edc65– edc652016年01月05日 20:11:19 +00:00Commented Jan 5, 2016 at 20:11
-
2\$\begingroup\$ @msh210 The code-golf tag implies shortest code in bytes if not otherwise stated. I'm trying to apply the DRY principle ;-). Similarly, unless otherwise stated, entries may be programs or functions. \$\endgroup\$Digital Trauma– Digital Trauma2016年01月05日 20:29:29 +00:00Commented Jan 5, 2016 at 20:29
-
1\$\begingroup\$ Must traling whitespace be exactly as shown? (i.e.2 spaces on the first line, none on the others) \$\endgroup\$Level River St– Level River St2016年01月05日 21:00:50 +00:00Commented Jan 5, 2016 at 21:00
-
1\$\begingroup\$ But here the meme actually makes sense due to the bandwidth restrictions involved! (Before setting up the ACII system, mission control was sending one bit at a time.) \$\endgroup\$ballesta25– ballesta252016年01月06日 22:29:56 +00:00Commented Jan 6, 2016 at 22:29
17 Answers 17
JavaScript (ES6), 323 (削除) 332 353 (削除ここまで)
Edit I managed to shorten this a bit
Step 1, believe it or not, using an array of 16 chars is shorter than toString
+ toUpperCase
Step 2, fiddling around to use .map
instead of for
, so that I can transform it all to a single expression function, avoding console.log and/or return.
Edit 2 Moved DEL at place 0, idea borrowed from Cole Cameron
H=x=>(x=[...'0123456789ABCDEF']).map((d,r)=>x[S='slice'](0,8).map(c=>(o+=` ${w=c*16+r} ${c+d} `[S](-z)+("DELNULSOHSTXETXEOTENQACKBELBS HT LF VT FF CR SO SI DLEDC1DC2DC3DC4NAKSYNETBCANEM SUBESCFS GS RS US ".substr((w+1&127)*3,3)||String.fromCharCode(w)),z=c<5?8:9,'Dec Hex '[S](0,c<2?z+2:z)),o+=`
`,z=7).join` `,o='')[0]+o
MD5: 41B6ECDE6A3A1324BE4836871D8354FE
Pixel perfect, I'd say
LESS GOLFED
H=x=>(
x=[...'0123456789ABCDEF'],
a="DELNULSOHSTXETXEOTENQACKBELBS HT LF VT FF CR SO SI DLEDC1DC2DC3DC4NAKSYNETBCANEM SUBESCFS GS RS US ",
o='', // the string o will contain the body (16 rows)
x.map((d,r)=> ( // main loop, r is thr row number, d is the second hex digit to print
o+=`\n`, z=7,
x.slice(0,8).map(c=> // loop on 8 columns, c is both column number and digit to print
(
// append the column to o
o += ` ${w=c*16+r} ${c+d} `.slice(-z) + (a.substr((w+1&127)*3,3)||String.fromCharCode(w)),
z=c<5?8:9, // adjust the column size
'Dec Hex '.slice(0,c<2?z+2:z) // column head, right size
) // the .map result is the 8 columns heading
).join` ` // join the heading in a single string
))[0] // any element of the result map is the heading
+ o // concatenate the body
)
Test
H=x=>(x=[...'0123456789ABCDEF']).map((d,r)=>x[S='slice'](0,8).map(c=>(o+=` ${w=c*16+r} ${c+d} `[S](-z)+("DELNULSOHSTXETXEOTENQACKBELBS HT LF VT FF CR SO SI DLEDC1DC2DC3DC4NAKSYNETBCANEM SUBESCFS GS RS US ".substr((w+1&127)*3,3)||String.fromCharCode(w)),z=c<5?8:9,'Dec Hex '[S](0,c<2?z+2:z)),o+=`
`,z=7).join` `,o='')[0]+o
/* TEST */
console.log=x=>O.textContent=x
console.log(H())
<pre id=O></pre>
C, (削除) 307 (削除ここまで) (削除) 310 (削除ここまで) (削除) 308 (削除ここまで) (削除) 307 (削除ここまで) 305 bytes
Finally working 100%.
i,j,z=127;main(){for(;j++<8;)printf("Dec Hex%*s",j<3?4:j<6||j>7?2:3,"");for(;i<143&&putchar(!i|i>z?10:32);i+=16)i=i>z?i%z:i,printf("%*d %02X ",i>95?4:3,i,i),i%z>31?putchar(i):printf("%.3s","DELNULSOHSTXETXEOTENQACKBELBS HT LF VT FF CR SO SI DLEDC1DC2DC3DC4NAKSYNETBCANEM SUBESCFS GS RS US "+(i+1)%128*3);}
Output:
$ ./a.out
Dec Hex Dec Hex Dec Hex Dec Hex Dec Hex Dec Hex Dec Hex Dec Hex
0 00 NUL 16 10 DLE 32 20 48 30 0 64 40 @ 80 50 P 96 60 ` 112 70 p
1 01 SOH 17 11 DC1 33 21 ! 49 31 1 65 41 A 81 51 Q 97 61 a 113 71 q
2 02 STX 18 12 DC2 34 22 " 50 32 2 66 42 B 82 52 R 98 62 b 114 72 r
3 03 ETX 19 13 DC3 35 23 # 51 33 3 67 43 C 83 53 S 99 63 c 115 73 s
4 04 EOT 20 14 DC4 36 24 $ 52 34 4 68 44 D 84 54 T 100 64 d 116 74 t
5 05 ENQ 21 15 NAK 37 25 % 53 35 5 69 45 E 85 55 U 101 65 e 117 75 u
6 06 ACK 22 16 SYN 38 26 & 54 36 6 70 46 F 86 56 V 102 66 f 118 76 v
7 07 BEL 23 17 ETB 39 27 ' 55 37 7 71 47 G 87 57 W 103 67 g 119 77 w
8 08 BS 24 18 CAN 40 28 ( 56 38 8 72 48 H 88 58 X 104 68 h 120 78 x
9 09 HT 25 19 EM 41 29 ) 57 39 9 73 49 I 89 59 Y 105 69 i 121 79 y
10 0A LF 26 1A SUB 42 2A * 58 3A : 74 4A J 90 5A Z 106 6A j 122 7A z
11 0B VT 27 1B ESC 43 2B + 59 3B ; 75 4B K 91 5B [ 107 6B k 123 7B {
12 0C FF 28 1C FS 44 2C , 60 3C < 76 4C L 92 5C \ 108 6C l 124 7C |
13 0D CR 29 1D GS 45 2D - 61 3D = 77 4D M 93 5D ] 109 6D m 125 7D }
14 0E SO 30 1E RS 46 2E . 62 3E > 78 4E N 94 5E ^ 110 6E n 126 7E ~
15 0F SI 31 1F US 47 2F / 63 3F ? 79 4F O 95 5F _ 111 6F o 127 7F DEL$ ./a.out > file.txt
$ md5sum file.txt
41b6ecde6a3a1324be4836871d8354fe file.txt
Ungolfed:
/* some variables for the trip */
i,j,z=127;
main()
{
/* print header row */
for(;j++<8;)
printf("Dec Hex%*s", j<3?4:j<6||j>7?2:3, "");
/* Iterate through ASCII values, print a space after every column, newline after every 8th value */
for(;i<143 && putchar(!i|i>z ? 10 : 32); i+=16)
{
/* print dec/hex value */
printf("%*d %02X ", i>95?4:3, i, i=i>z?i%z:i);
/* print character or friendly name for non-printable characters */
i%z>31
? putchar(i)
: printf("%.3s", "DELNULSOHSTXETXEOTENQACKBELBS HT LF VT FF CR SO SI DLEDC1DC2DC3DC4NAKSYNETBCANEM SUBESCFS GS RS US "+(i+1)%128*3);
}
}
Try it on Ideone.
Edit: 2 more bytes. Many thanks to Dan Allen and Digital Trauma.
-
2\$\begingroup\$ Also, you don't need the
#include <stdio.h>
\$\endgroup\$Digital Trauma– Digital Trauma2016年01月05日 22:01:14 +00:00Commented Jan 5, 2016 at 22:01 -
\$\begingroup\$ @DigitalTrauma: You can in GCC. You can't in C. \$\endgroup\$DevSolar– DevSolar2016年01月06日 08:36:33 +00:00Commented Jan 6, 2016 at 8:36
-
2\$\begingroup\$ @DevSolar It's not a GCC thing. In K&R C, the compiler, not having the declaration of printf and putchar, will guess, and guess right. On the other hand, it's undefined behaviour in standard C99, but who cares - it works as expected in all compilers that I know \$\endgroup\$edc65– edc652016年01月06日 09:00:40 +00:00Commented Jan 6, 2016 at 9:00
-
1\$\begingroup\$ 305 if you change the first line to
x,y,z=127;
and replace the 4 occurences of 127 with z. Adds 6 characters but takes 8 away. \$\endgroup\$Persixty– Persixty2016年01月07日 09:05:34 +00:00Commented Jan 7, 2016 at 9:05 -
\$\begingroup\$ Clever trick, putting the DEL at first place. I'll borrow this idea. I would upvote now just for this one, but I already upvoted yesterday :) \$\endgroup\$edc65– edc652016年01月08日 09:02:56 +00:00Commented Jan 8, 2016 at 9:02
Bubblegum, 535 bytes
0000000: e0 05 2f 02 0f 5d 00 22 19 48 62 01 d3 1f 78 e2 ../..].".Hb...x.
0000010: 9a a0 8e 4e 5d d1 b4 c1 77 aa 32 58 ca 97 55 7e ...N]...w.2X..U~
0000020: a8 01 87 7d db e4 00 55 8f c0 49 67 b6 56 02 5e ...}...U..Ig.V.^
0000030: ae b2 4d e2 a9 f4 7f 99 a8 56 9e b7 4c 60 a4 79 ..M......V..L`.y
0000040: 6a 76 54 11 90 72 d6 b7 19 df 2f 57 39 2d 21 c0 jvT..r..../W9-!.
0000050: d1 4c 5e d6 21 29 c8 ed 7c a9 7b 8c 85 dc 62 a1 .L^.!)..|.{...b.
0000060: 65 98 e1 0b a7 36 83 c8 ca 88 0c 57 22 f6 56 1e e....6.....W".V.
0000070: 45 03 b6 74 21 a8 39 52 e9 71 b4 98 ed 71 38 9f E..t!.9R.q...q8.
0000080: 2d dc 21 d7 bf 60 41 cc bb bd a7 cb 0b 17 8d 65 -.!..`A........e
0000090: 05 13 04 0f 6c bb 67 62 aa c7 ad 6b be 9e 46 77 ....l.gb...k..Fw
00000a0: 35 b9 91 85 f5 47 31 2f c7 ec da c0 00 0e a6 48 5....G1/.......H
00000b0: 01 ba 8b cd b0 34 81 c4 74 9f 4e 3b c3 d0 f7 10 .....4..t.N;....
00000c0: 46 a0 55 8d 49 5d b7 b0 c9 79 ac e5 5f ef 49 f2 F.U.I]...y.._.I.
00000d0: b0 1b 71 3a e1 30 7a fc ce a7 a8 d5 c3 9a 35 1a ..q:.0z.......5.
00000e0: 4e 27 92 40 4b b5 9b c4 0d 5c e8 cd 71 00 bd c1 N'.@K....\..q...
00000f0: ca aa d2 05 dc e1 0f d9 19 1d 6f 14 87 b3 e4 e8 ..........o.....
0000100: 9e 82 64 d8 e4 76 e7 24 0a 0e 88 72 a1 12 44 95 ..d..v.$...r..D.
0000110: d4 78 82 bd da 71 f3 fb 03 00 d1 4b c8 80 cb 49 .x...q.....K...I
0000120: 0b 98 be 26 ba 3e e8 82 e2 14 9b ba 1a cf bf bc ...&.>..........
0000130: 30 4e c4 e8 7e b4 d5 46 e6 bc 73 97 c5 ed a6 e2 0N..~..F..s.....
0000140: 06 02 e7 1b 74 4d da 73 fb 15 68 50 c0 ed 32 9b ....tM.s..hP..2.
0000150: 0d d7 49 d5 c1 a2 e9 07 2c 77 81 6c d3 8d 59 26 ..I.....,w.l..Y&
0000160: 1c 35 ec 2b 7e cb 3a f1 cc 45 a9 e5 6d 3e 33 ca .5.+~.:..E..m>3.
0000170: 56 3c 8a 8d f6 13 e9 59 d4 52 07 44 ab 5e bc f4 V<.....Y.R.D.^..
0000180: 1f ed f8 9c 8b 48 e1 c4 6c fd 47 d5 04 cc 6e aa .....H..l.G...n.
0000190: 3f 54 b8 cc cd 09 01 6d 20 3c 42 c9 44 da b1 c1 ?T.....m <B.D...
00001a0: 69 80 12 26 6b 65 e1 4d 1c c3 48 36 2b 14 00 61 i..&ke.M..H6+..a
00001b0: 04 6b 9a 59 2a 53 e3 64 a7 4f dd cc be 2c 20 5e .k.Y*S.d.O..., ^
00001c0: f7 c7 64 34 e6 12 a6 44 c1 69 35 76 05 db 13 ab ..d4...D.i5v....
00001d0: 52 10 b5 8e da 8e c5 3c 4c d0 69 0b 19 18 67 ef R......<L.i...g.
00001e0: 44 1c 7b 70 63 98 95 40 28 6e 3d e7 44 cb 24 83 D.{pc..@(n=.D.$.
00001f0: 88 62 63 3c 02 1c e7 db db 02 56 ae cd 9c e0 9c .bc<......V.....
0000200: 1c a1 c1 ae d1 dd 7b b7 e6 bd 5b 38 ee 75 c5 6c ......{...[8.u.l
0000210: 06 16 6c b2 fb 00 00 ..l....
This above program uses LZMA compression. Try it online!
-
1\$\begingroup\$ I think you can do better than that... ;-) \$\endgroup\$Digital Trauma– Digital Trauma2016年01月05日 20:08:40 +00:00Commented Jan 5, 2016 at 20:08
-
11\$\begingroup\$ Not in Bubblegum... \$\endgroup\$Dennis– Dennis2016年01月05日 20:10:21 +00:00Commented Jan 5, 2016 at 20:10
-
4\$\begingroup\$ How many bytes would you need to print the entire novel The Martian? \$\endgroup\$Alex A.– Alex A.2016年01月05日 21:31:58 +00:00Commented Jan 5, 2016 at 21:31
-
1\$\begingroup\$ @DigitalTrauma It's easy... You only have to execute a prefix attack on SHA-256... \$\endgroup\$LegionMammal978– LegionMammal9782016年01月06日 12:15:28 +00:00Commented Jan 6, 2016 at 12:15
C, (削除) 385 (削除ここまで) (削除) 384 (削除ここまで) 358 bytes
i,n,j;char a[100]="NULSOHSTXETXEOTENQACKBELBS HT LF VT FF CR SO SI DLEDC1DC2DC3DC4NAKSYNETBCANEM SUBESCFS GS RS US DEL";z(o){printf("%3d %02X %.*s ",j,j,3,a+o);}main(){for(;i<8;i++){printf("%3s %3s ","Dec","HEX ");}printf("\n");for(;n<16;n++){for(j=n;j<=n+112;j+=16){if(j==127)z(96);else j<32?z(j*3):printf("%3d %02X %c ",j,j,j);}printf("\n");}return 0;}
The guy above beat me to the punch but I still wanted to submit because I enjoyed this one.
De-golfed:
#include<stdio.h>
i,n,j;
char a[100] = "NULSOHSTXETXEOTENQACKBELBS HT LF VT FF CR SO SI DLEDC1DC2DC3DC4NAKSYNETBCANEM SUBESCFS GS RS US DEL";
z(o){printf("%3d %02X %.*s ",j,j,3,a+o);}
main(){
for(;i<8;i++){printf("%3s %3s ","Dec","HEX ");}printf("\n");
for(;n<16;n++){
for(j=n;j<=n+112;j+=16){
if(j==127)z(96);
else j<32?z(j*3):printf("%3d %02X %c ",j,j,j);
}
printf("\n");
}
return 0;
}
UPDATE : replaced a var with j. Saved a byte ;)
UPDATE2: Trimmed a few extra thingys and functionized a print thingy to save some bytes.
-
\$\begingroup\$ Similar to many answers, you don't have the correct spacing between columns. Also
s/HEX/Hex/
. \$\endgroup\$Digital Trauma– Digital Trauma2016年01月06日 01:26:30 +00:00Commented Jan 6, 2016 at 1:26 -
\$\begingroup\$ I must have missed the chapter explaining the
thingy
c keyword ;-P \$\endgroup\$Digital Trauma– Digital Trauma2016年01月06日 16:17:42 +00:00Commented Jan 6, 2016 at 16:17
JavaScript ES6 (削除) 432 (削除ここまで) (削除) 405 (削除ここまで) (削除) 398 (削除ここまで) 385
o="Dec Hex "
o=`${o} ${o} ${o+o+o+o} ${o} ${o}\n`
for(i=0;i<16;i++){for(j=0;j<8;j++){k=i+j*16
o+=k>9&&k<16?' ':k<96||k>99?' ':' '
o+=k+" "+(k<16?0:'')+k.toString(16).toUpperCase()+" "
o+=k>31?String.fromCharCode(k):"NULSOHSTXETXEOTENQACKBELBS HT LF VT FF CR SO SI DLEDC1DC2DC3DC4NAKSYNETBCANEM SUBESCFS GS RS US ".substr(k*3,3)
}o+="\n"}
console.log(o.substr(0,o.length-2)+'DEL')
-
\$\begingroup\$ ES5 I take it? ES6 you could do things like
${o} ${o} ${o+o+o+o} ${o} ${o}
. \$\endgroup\$Neil– Neil2016年01月06日 18:04:37 +00:00Commented Jan 6, 2016 at 18:04 -
\$\begingroup\$ @Neil Thanks! I added the change and the trailing space is taken care of. \$\endgroup\$wolfhammer– wolfhammer2016年01月06日 19:12:27 +00:00Commented Jan 6, 2016 at 19:12
-
\$\begingroup\$ Actually you can go one step further since literal newlines are legal inside backticks (not sure how to demonstrate that in a comment). \$\endgroup\$Neil– Neil2016年01月07日 11:52:36 +00:00Commented Jan 7, 2016 at 11:52
Golfscript, 225 bytes
"\x04\x04\x02\x02\x02\x03\x03\x02"{"Dec Hex"" ":s@*}%n[128,{3s*\+-4>s+.~.96<@>256円+16base{10,"ABCDEF"1/+=}%1>{+}/}%"\x1f\xbb\x89\xbc\xaf\x82=F\xd7U%\x80\x8a\xf6\xc7U\xa6al)A\xf6\x19\xfe\x03\x83\xef-\x9f\xe7m\xeb\x82h\xf3\xbfEm6V\x1fD\x8c\xd7~\xcb\x95&(\x1e/:\x8e\xc5\xb0\x0b\xd6\xd4\xd09\xdd""\xff\x1e"{base}/{90,65>1342s++1/=}%3/32/"\x7f"{,32>}%1/*]zip{s*}%16/zip{s*n}%
Python 2.7, 389 Bytes
Probably not going to try and trim this down anymore, but it was fun to get it this far.
r=range
c='NULSOHSTXETXEOTENQACKBELBS HT LF VT FF CR SO SI DLEDC1DC2DC3DC4NAKSYNETBCANEM SUBESCFS GS RS US '
h=''
p=['']*16
o=[11,11,9,9,9,10,10,9]
e=['%3s '%str(i)+('%02X ')%(i)+('DEL',c[i*3:i*3+3].strip() if i<32 else chr(i))[i<127] for i in r(128)]
for i in r(8):
l=e[i*16:(i+1)*16]
h+='Dec Hex'.ljust(o[i])
p=[p[j]+l[j].ljust((0,o[i])[i<7]) for j in r(16)]
print h+'\n'+'\n'.join(p)
Python 3.4, 216 Bytes
Also valid Python 2.7. Used FryAmTheEggman's idea/suggestion about curses.ascii.controlnames, which saves almost 100 bytes.
import curses.ascii as a
o=''
w=4,4,2,2,2,3,3,1
for x in w:o+='Dec Hex'+' '*x
o+=' '
for n in range(16):
o+='\n'
for x in w:o+='%3d %02X %-*s'%(n,n,x,n>31and[chr(n),'DEL'][n>126]or a.controlnames[n]);n+=16
print(o)
$ python ascii.py | md5
58824a1dd7264c0410eb4d727aec54e1
Ruby (2.2.2p95), 277 (削除) 295 306 331 364 (削除ここまで)
a='Dec Hex ';puts"#{a} "*2+a*4+" #{a}"*2,16.times.map{|i|8.times.map{|j|(k=i+j*16;k==127?' 127 7F DEL':"#{k} #{'%.2X'%k} #{'NULSOHSTXETXEOTENQACKBELBS HT LF VT FF CR SO SI DLEDC1DC2DC3DC4NAKSYNETBCANEM SUBESCFS GS RS US '[k*3..k*3+2]||k.chr}").rjust(j<2?10+j :9+j/6)}.join}
ungolfed
s = "Dec Hex " * 2 + "Dec Hex " * 4 + " Dec Hex " * 2
a = 127.times.map { |i|
"#{i} #{'%.2X'%i} #{'NULSOHSTXETXEOTENQACKBELBS HT LF VT FF CR SO SI DLEDC1DC2DC3DC4NAKSYNETBCANEM SUBESCFS GS RS US '[i*3..i*3+2]||i.chr}"
}
a << ' 127 7F DEL'
x = 16.times.map { |i|
8.times.map { |j|
a[i + j * 16].rjust(j < 2 ? 10 + j : 9 + j / 6)
}.join
}.join "\n"
puts s, x
-
\$\begingroup\$ The control name array really kills me here. Still hoping to find something similar to Python's curses.ascii for Ruby... \$\endgroup\$Connor Clark– Connor Clark2016年01月19日 21:44:57 +00:00Commented Jan 19, 2016 at 21:44
Microscript II, 1314 bytes
Probably far from optimal.
"Dec Hex "pp"Dec Hex "ppps"Dec Hex "ppoP" 0 00 NUL 16 10 DLE 32 20 48 30 0 64 40 @ 80 50 P 96 60 ` 112 70 p\n 1 01 SOH 17 11 DC1 33 21 ! 49 31 1 65 41 A 81 51 Q 97 61 a 113 71 q\n 2 02 STX 18 12 DC2 34 22 \" 50 32 2 66 42 B 82 52 R 98 62 b 114 72 r\n 3 03 ETX 19 13 DC3 35 23 # 51 33 3 67 43 C 83 53 S 99 63 c 115 73 s\n 4 04 EOT 20 14 DC4 36 24 $ 52 34 4 68 44 D 84 54 T 100 64 d 116 74 t\n 5 05 ENQ 21 15 NAK 37 25 % 53 35 5 69 45 E 85 55 U 101 65 e 117 75 u\n 6 06 ACK 22 16 SYN 38 26 & 54 36 6 70 46 F 86 56 V 102 66 f 118 76 v\n 7 07 BEL 23 17 ETB 39 27 ' 55 37 7 71 47 G 87 57 W 103 67 g 119 77 w\n 8 08 BS 24 18 CAN 40 28 ( 56 38 8 72 48 H 88 58 X 104 68 h 120 78 x\n 9 09 HT 25 19 EM 41 29 ) 57 39 9 73 49 I 89 59 Y 105 69 i 121 79 y\n 10 0A LF 26 1A SUB 42 2A * 58 3A : 74 4A J 90 5A Z 106 6A j 122 7A z\n 11 0B VT 27 1B ESC 43 2B + 59 3B ; 75 4B K 91 5B [ 107 6B k 123 7B {\n 12 0C FF 28 1C FS 44 2C , 60 3C < 76 4C L 92 5C \\ 108 6C l 124 7C |\n 13 0D CR 29 1D GS 45 2D - 61 3D = 77 4D M 93 5D ] 109 6D m 125 7D }\n 14 0E SO 30 1E RS 46 2E . 62 3E > 78 4E N 94 5E ^ 110 6E n 126 7E ~\n 15 0F SI 31 1F US 47 2F / 63 3F ? 79 4F O 95 5F _ 111 6F o 127 7F DEL"
-
2\$\begingroup\$ How do I test this? \$\endgroup\$Digital Trauma– Digital Trauma2016年01月06日 16:18:21 +00:00Commented Jan 6, 2016 at 16:18
-
\$\begingroup\$ @DigitalTrauma I'll add a link to the github repo to the answer. Run the interpreter with the program as the first line of the input. \$\endgroup\$SuperJedi224– SuperJedi2242016年01月06日 20:04:51 +00:00Commented Jan 6, 2016 at 20:04
-
\$\begingroup\$ Congrats. You made the most boring answer on this contest. ò_ó +1 \$\endgroup\$user48538– user485382016年01月07日 19:40:12 +00:00Commented Jan 7, 2016 at 19:40
-
\$\begingroup\$ @zyabin101 Well, it is about 30 bytes shorter than simply printing the string all at once would likely be. \$\endgroup\$SuperJedi224– SuperJedi2242016年01月07日 20:11:12 +00:00Commented Jan 7, 2016 at 20:11
JavaScript, (削除) 415 (削除ここまで) (削除) 413 (削除ここまで) (削除) 4231 (削除ここまで) (削除) 411 (削除ここまで) (削除) 406 (削除ここまで) (削除) 402 (削除ここまで) (削除) 4142 (削除ここまで) 412 bytes
x=>eval('a=`${d="Dec Hex "} `[r="repeat"](2)+d[r](3)+`${d} `[r](2)+d+`
`;z="NULSOHSTXETXEOTENQACKBELBS HT LF VT FF CR SO SI DLEDC1DC2DC3DC4NAKSYNETBCANEM SUBESCFS GS RS US ".match(/.{3}/g);z[127]="DEL";for(j=i=0;i<128;i++){a+=(" "+((b=i%8*16)+j)).slice(-3)+" "+(0+(c=b+j).toString(16).toUpperCase()).slice(-2)+" "+(c<32||i==127?z[c]:String.fromCharCode(c))+(b==112?`
`:(b < 80 ? " " : " "));if(b==112)j++}a')
I couldn't figure out how to print the chars prior to char code 32, so just listed them as a string.
The hash I've got seems to match (41b6ecde6a3a1324be4836871d8354fe
).
Demo + Ungolfed:
function t() {
a = `${d="Dec Hex "} `.repeat(2) + d.repeat(3) + `${d} `.repeat(2) + d + "\n";
z = "NULSOHSTXETXEOTENQACKBELBS HT LF VT FF CR SO SI DLEDC1DC2DC3DC4NAKSYNETBCANEM SUBESCFS GS RS US ".match(/.{3}/g);
z[127] = "DEL";
for (j = i = 0; i < 128; i++) {
a += (" " + ((b = i % 8 * 16) + j)).slice(-3) + " " + (0 + (c = b + j).toString(16).toUpperCase()).slice(-2) + " " + (c < 32 || i == 127 ? z[c] : String.fromCharCode(c)) + (b==112?"\n":(b < 80 ? " " : " "));
if(b==112)j++;
}
return a
}
document.write("<pre>" + t() + "</pre>")
1 - fixed spacing
2 - again fixed spacing
-
\$\begingroup\$ Huh, in mobile Chrome <pre> doesn't monospace, though it should. \$\endgroup\$nicael– nicael2016年01月05日 20:55:50 +00:00Commented Jan 5, 2016 at 20:55
-
\$\begingroup\$ I don't think that's qute the spacing the question's asking for. \$\endgroup\$SuperJedi224– SuperJedi2242016年01月05日 21:00:09 +00:00Commented Jan 5, 2016 at 21:00
-
\$\begingroup\$ @Dig oy wtf. I see this 0.0 \$\endgroup\$nicael– nicael2016年01月05日 21:04:24 +00:00Commented Jan 5, 2016 at 21:04
-
\$\begingroup\$ @Digital I've fixed the spacing. \$\endgroup\$nicael– nicael2016年01月06日 07:57:33 +00:00Commented Jan 6, 2016 at 7:57
-
\$\begingroup\$ @Digital You after the last column? Ok, but that doesn't even affect the appearance... \$\endgroup\$nicael– nicael2016年01月06日 16:14:47 +00:00Commented Jan 6, 2016 at 16:14
MATLAB, 363 bytes
Not as small as C but comparable...
h='';s=@sprintf;for i=[4 4 2 2 2 3 3 2]h=s([h 'Dec Hex%*s'],i,'');end
h=[h 10];a='NULDLESOHDC1STXDC2ETXDC3EOTDC4ENQNAKACKSYNBELETBBS CANHT EM LF SUBVT ESCFF FS CR GS SO RS SI US ';for i=0:15for j=0:7k=i+16*j;if j<2b=1+6*i+3*j;h=[h s('%3d %02X %-3s ',k,k,a(b:b+2))];else
h=[h s('%*d %02X %c ',3+(j>5),k,k,k)];end
end
h=[h(1:end-1) 10];end
disp([h(1:end-2) 'DEL']);
-
\$\begingroup\$ Do I have to download the free trial, or is there some other way I can test your answer? Or can you claim the md5 of the output is correct? \$\endgroup\$Digital Trauma– Digital Trauma2016年01月06日 16:15:45 +00:00Commented Jan 6, 2016 at 16:15
-
\$\begingroup\$ You can run it in GNU Octave, but I already did and this version lacks the additional padding between columns 6, 7 and 8. Apart from that, it's correct. \$\endgroup\$Rainer P.– Rainer P.2016年01月06日 18:14:12 +00:00Commented Jan 6, 2016 at 18:14
-
\$\begingroup\$ Yes, you are right. Corrected the code. This happens, if you have the golfed and ungolfed variant side by side. Corrected some typos. Interestingly I get a different ascii|tail -n=+7 (first row last two spaces are missing) (Ubuntu 15.10). I never noticed, that in matlab variables named d and e behave differently. (for i=1:2d=10;end; % Syntax error) (for i=1:2k=10;end; % Just fine) \$\endgroup\$Jonas– Jonas2016年01月07日 17:23:40 +00:00Commented Jan 7, 2016 at 17:23
///, 998 bytes
//\/\//// /Dec Hex/ 0/ 1/ 2/ 3/ 4/ 5/ 6 / 7/ 8/ 9/0 /1 /2 /3 /4 /5 /6 /7 /8 /9 /A /B /C /D /E /F /
/ NUL 6DL280 4@ 0P6` 12 pSOH 7DC3! 91 51Q7a 13 qSTX 8DC4" 02 62R8b 14 rETX 9DC5# 13 73S9c 15 sEOT 0DC6$ 24 84T 00d 16 tENQ 1NAK 7% 35 95U 01e 17 uACK 2SYN 8& 46 06V 02f 18 v BEL 3ET9' 57 1G 7W 03g 19 wBS4CAN 0( 68 2H 8X 04h 20 xHT5EM1) 79 3I 9Y 05i 21 y
0L 6SU2* 8: 4J 0Z 06j 22 z
1VT7ES3+ 9; 5K 1[ 07k 23 {
2F 8FS4, 0< 6L 2\\ 08l 24 |
3CR9GS5- 1= 7M 3] 09m 25 }
4SO0RS6. 2> 8N 4^ 10n 26 ~
5SI1US7\/ 3? 9O 5_ 11o 27 DEL
PHP, (削除) 330 (削除ここまで) 321 bytes
$a=[NUL,SOH,STX,ETX,EOT,ENQ,ACK,BEL,BS,HT,LF,VT,FF,CR,SO,SI,DLE,DC1,DC2,DC3,DC4,NAK,SYN,ETB,CAN,EM,SUB,ESC,FS,GS,RS,US,127=>DEL];foreach($t=[3,3,1,1,1,2,2,0]as$i)echo'Dec Hex ',str_repeat(' ',$i);echo"
";for($x=0;$x<16;)for($y=$x++;$y<128;$y+=16)printf("% 3d %102ドルX % -".$t[$y/16].s.($y<112?' ':"
"),$y,$a[$y]?:chr($y));
Less golfy:
$a=[NUL,SOH,STX,ETX,EOT,ENQ,ACK,BEL,BS,HT,LF,VT,FF,CR,SO,SI,DLE,
DC1,DC2,DC3,DC4,NAK,SYN,ETB,CAN,EM,SUB,ESC,FS,GS,RS,US,127=>DEL];
$t=[3,3,1,1,1,2,2,0];
foreach( $t as $i )
echo 'Dec Hex ', str_repeat( ' ', $i );
echo" \n";
for( $x=0; $x<16; ) {
for( $y=$x++; $y<128; $y+=16 ) {
printf( "% 3d %102ドルX % -" . $t[$y/16] . 's' . ( $y<112 ?' ' : "\n" ),
$y, $a[$y] ?: chr($y) );
}
}
Vyxal 3 -Ṁ
, 145 bytes
"GΩ§ḃ2x=k'*ƓRSLE„"dXvB3↑&ȯḊṚ„"dledc1dc2dc3dc4""ʂ4mpṪ»F5qṬURḤ„"W`Qγ0„W~L~fzẆfɾ3»kPfJ"DEL"J6ƛ∦λ95>34inɸ«}ЧHɾ2Þ0]$Zvfv„1ẆT"e~꣄1⁄2\⊻V8Y"c×ばつI∑wp'
It's Vyxal time!
"GΩ§ḃ2x=k'*ƓRSLE„"dXvB3↑&ȯḊṚ„"dledc1dc2dc3dc4""ʂ4mpṪ»F5qṬURḤ„"W`Qγ0„
W~L~fzẆfɾ3»kPfJ"DEL"J6ƛ∦λ95>34inɸ«}ЧHɾ2Þ0]$Zvfv„1ẆT"e~꣄1⁄2\⊻V8Y"c×ばつI∑wp'
"GΩ§ḃ2x=k'*ƓRSLE„"dXvB3↑&ȯḊṚ„"dledc1dc2dc3dc4""ʂ4mpṪ»F5qṬURḤ„"W`Qγ0„ # push unprintables as strings
# wrap stack into list
W
~L~f # [3,2,3,3,2]
zẆf # wrap each string into chunks of length specified by digit
ɾ3» # Uppercase and add spaces to make each length 3
kPfJ # append printable ascii
"DEL"J # append "DEL"
6ƛ∦ ] # for each number in 0-127 do both...
λ95>34inɸ«} # If less than 95, prepend 3 spaces, else prepend 4
ЧHɾ2Þ0 # convert to hexadecimal, uppercased, with zeros prepended to make it length 2
$Zvfv„ # Zip dec/hex pair with corresponding ascii string, then join on spaces into one string
1ẆT # Group into 16, transpose
"e~꣄ # compressed "dechex"
1⁄2\⊻V # Split in half, joined on spaces, titlecased
8Y # repeated 8 times
"c⌐^q" # 44222332
×ばつ # spaces times each digit
I∑ # Interleaved with the "Dec Hex" list and merged
wp' # prepend header, join the whole list on spaces and newlines
💎
Created with the help of Luminespire.
Common Lisp (SBCL), 309
(progn(format t"~{~v,0TDec Hex ~}"#1='(0 11 22 31 40 49 59 69))(dotimes(y 16)(fresh-line)(loop for x from 0 for k in #1#for i =(+(* 16 x)y)do(format t"~v,0T~3d ~2,'0x ~a"k i i(if(graphic-char-p #2=(code-char i))#2#(case i(8"BS")(9"HT")(10"LF")(12"FF")(13"CR")(127"DEL")(t(string-upcase(char-name #2#)))))))))
A script to be run which outputs the following to standard output, ie. the version without a final newline (md5: 41b6ecde6a3a1324be4836871d8354fe).
Dec Hex Dec Hex Dec Hex Dec Hex Dec Hex Dec Hex Dec Hex Dec Hex
0 00 NUL 16 10 DLE 32 20 48 30 0 64 40 @ 80 50 P 96 60 ` 112 70 p
1 01 SOH 17 11 DC1 33 21 ! 49 31 1 65 41 A 81 51 Q 97 61 a 113 71 q
2 02 STX 18 12 DC2 34 22 " 50 32 2 66 42 B 82 52 R 98 62 b 114 72 r
3 03 ETX 19 13 DC3 35 23 # 51 33 3 67 43 C 83 53 S 99 63 c 115 73 s
4 04 EOT 20 14 DC4 36 24 $ 52 34 4 68 44 D 84 54 T 100 64 d 116 74 t
5 05 ENQ 21 15 NAK 37 25 % 53 35 5 69 45 E 85 55 U 101 65 e 117 75 u
6 06 ACK 22 16 SYN 38 26 & 54 36 6 70 46 F 86 56 V 102 66 f 118 76 v
7 07 BEL 23 17 ETB 39 27 ' 55 37 7 71 47 G 87 57 W 103 67 g 119 77 w
8 08 BS 24 18 CAN 40 28 ( 56 38 8 72 48 H 88 58 X 104 68 h 120 78 x
9 09 HT 25 19 EM 41 29 ) 57 39 9 73 49 I 89 59 Y 105 69 i 121 79 y
10 0A LF 26 1A SUB 42 2A * 58 3A : 74 4A J 90 5A Z 106 6A j 122 7A z
11 0B VT 27 1B ESC 43 2B + 59 3B ; 75 4B K 91 5B [ 107 6B k 123 7B {
12 0C FF 28 1C FS 44 2C , 60 3C < 76 4C L 92 5C \ 108 6C l 124 7C |
13 0D CR 29 1D GS 45 2D - 61 3D = 77 4D M 93 5D ] 109 6D m 125 7D }
14 0E SO 30 1E RS 46 2E . 62 3E > 78 4E N 94 5E ^ 110 6E n 126 7E ~
15 0F SI 31 1F US 47 2F / 63 3F ? 79 4F O 95 5F _ 111 6F o 127 7F DEL
Details
(0 11 22 31 40 49 59 69)
contains the position of each column in a line, as obtained by inspecting the desired output. There were many corner cases that are simply better handled by hard-coding the exact position for each column and letting the~T
format directive tabulate as needed.Since names of characters are implementation-dependent, this code targets only the SBCL interpreter (I tested with CCL, which uses other names). I rely on
char-name
for most non-graphic characters, but some of them have explicit mappings to the expected output.
Ungolfed
(progn
(format t"~{~v,0TDec Hex ~}" '(0 11 22 31 40 49 59 69))
(dotimes(y 16)
(fresh-line)
(loop for x from 0
for k in '(0 11 22 31 40 49 59 69)
for i = (+ (* 16 x) y)
for c = (code-char i)
do (format t
"~v,0T~3d ~2,'0x ~a"
k
i
i
(if (graphic-char-p c)
c
(case i
(8"BS")
(9"HT")
(10"LF")
(12"FF")
(13"CR")
(127"DEL")
(t (string-upcase (char-name c)))))))))
Perl 5, 271 bytes
$h="Dec Hex ";say"$h "x2,$h x4," $h $h";map{//;printf"%3d %02X %-4s"x2 ."%3d %02X %s "x4 ."%4d %02X %s "x2 .$/,map{$_=16*$_+$',$_,('NULSOHSTXETXEOTENQACKBELBS HT LF VT FF CR SO SI DLEDC1DC2DC3DC4NAKSYNETBCANEM SUBESC'=~/.../g,FS,GS,RS,US,(0)x95,DEL)[$_]||chr}0..7}0..15