Recreate the ASCII-table as an ASCII-table

26

3

Frequently while I'm code-golfing, I'll want to know what the ASCII value of a certain character is. One of my favorite resources for quickly looking up all of the printable ASCII characters is ASCIItable.com. This has a really nice image that not only shows the printable ASCII characters and their values, but also the unprintable and extended characters, and the values in hexadecimal, octal, and HTML:

enter image description here

Today's challenge is to recreate that ASCII table as an ASCII table instead of an image. To keep things simpler, we will not use control-characters (characters below 32) and we'll only show the decimal value and the character. In other words, your challenge is to write either a full-program or a function that prints or returns the following text:

Dec  Chr   | Dec  Chr   | Dec  Chr
----------------------------------
32   Space | 64   @     | 96   `
33   !     | 65   A     | 97   a
34   "     | 66   B     | 98   b
35   #     | 67   C     | 99   c
36   $     | 68   D     | 100  d
37   %     | 69   E     | 101  e
38   &     | 70   F     | 102  f
39   '     | 71   G     | 103  g
40   (     | 72   H     | 104  h
41   )     | 73   I     | 105  i
42   *     | 74   J     | 106  j
43   +     | 75   K     | 107  k
44   ,     | 76   L     | 108  l
45   -     | 77   M     | 109  m
46   .     | 78   N     | 110  n
47   /     | 79   O     | 111  o
48   0     | 80   P     | 112  p
49   1     | 81   Q     | 113  q
50   2     | 82   R     | 114  r
51   3     | 83   S     | 115  s
52   4     | 84   T     | 116  t
53   5     | 85   U     | 117  u
54   6     | 86   V     | 118  v
55   7     | 87   W     | 119  w
56   8     | 88   X     | 120  x
57   9     | 89   Y     | 121  y
58   :     | 90   Z     | 122  z
59   ;     | 91   [     | 123  {
60   <     | 92   \     | 124  |
61   =     | 93   ]     | 125  }
62   >     | 94   ^     | 126  ~
63   ?     | 95   _     | 127  DEL

Trailing spaces on each line, and a trailing newline are permitted. Since this is a challenge, your submission may not take any input, or access any external resources (such as a file or the web), and your goal is to compress the code to output this text as much as possible.

Standard loopholes apply, and the shortest answer in bytes wins. Happy golfing!

James

Posted 2017-01-07T15:46:23.530

Reputation: 54 537

3I have that exact ASCII table image stuck on my wall... Anyway, can a list of lines be returned? – FlipTack – 2017-01-07T15:48:45.763

2@fliptack Of course you can. Why wouldn't I allow something perfectly reasonable like that? – James – 2017-01-07T15:57:15.370

10I wouldn't say this is a dupe - the other one requires hex values, names of unprintables, and is a different table format. This sticks to visible ASCII and allow golfier code by not asking for the 3-letter codes of all the unprintables. – FlipTack – 2017-01-07T16:12:03.857

@FlipTack It still has Space and DEL – simon – 2017-01-07T16:20:18.240

2@gurka yes, but the other one has every single control character. – FlipTack – 2017-01-07T16:42:38.027

Answers

11

Python 2, 185 183 175 159 bytes

Saved 8 bytes thanks to FlipTack!

Still quite new to golfing in Python.

for a in["Dec  Chr   | "*3,"-"*39]+["".join(["%-5d%-6s| "%(l,('Space',chr(l),'DEL')[(l>32)+(l>126)]) for l in(i,32+i,64+i)])for i in range(32,64)]:print a[:-5]

Uses a nested list comprehension to generate the table body.

Ungolfed:

lines =   \
  ["Dec  Chr   | "*3, "-"*39] +                    # first two lines
    ["".join(                                        # join 3 parts of each line
      ["%-5d%-6s| " % (l, ('Space',chr(l),'DEL')[(l>32)+(l>126)]) 
        for l in (i,32+i,64+i)]                      # generate 3 parts of a line
      )
      for i in range(32,64)]

for line in lines: print line[:-5]

Old attempt, 185 183 175 bytes

print("Dec  Chr   | "*3)[:-5]+"\n"+"-"*34
a=lambda x:('Space',chr(x),'DEL')[(x>32)+(x>126)]
for l in range(32,64):print("%-5d%-6s| "*3%(l,a(l),l+32,a(l+32),l+64,a(l+64)))[:-5]

Ungolfed:

print ("Dec  Chr   | "*3)[:-5] + "\n" + "-"*34
def a(x):
    return "Space" if x==32 else "DEL" if x==127 else chr(x)
for line in range(32,64):
    print ("%-5d%-6s| "*3 % (line, a(line), line+32, a(line+32), 
      line+64, a(line+64))) [:-5]

busukxuan

Posted 2017-01-07T15:46:23.530

Reputation: 2 728

I see a space at for l in(i,32+i,64+i)]), could be removed to save one byte. – maxb – 2018-04-25T08:37:06.340

6

Befunge, 176 172 bytes

<v"Dec  Chr   "0
^>:#,_$1+:2`#v_" |",,
\:#->#1_55+,v>55+,"!-":>,#:
+2*,:"_"`#@_v>1+:8-#v_$1+:3%!:7g,!29+*5
*84+1%3\/3::<^,gg00:<`"c"p00+5+`"~"\`*84::p62:.:+*
  Space

| DEL

Try it online!

James Holderness

Posted 2017-01-07T15:46:23.530

Reputation: 8 298

6

Pyth, 89 85 79 77 bytes

PP*"Dec  Chr   | "3*\-34V32PPsm++.[`=+N32;5.[?qN32"Space"?qN127"DEL"CN;6"| "3

Try it online!

insert_name_here

Posted 2017-01-07T15:46:23.530

Reputation: 816

5

JavaScript (ES6), 179 173 bytes

f=n=>n?(n>>6?' | ':`
`)+n+(99<n?'  ':'   ')+(126<n?'DEL':String.fromCharCode(n)+'    '+f(n>95?n-63:n+32)):`${x='Dec  Chr   '}| ${x}| ${x}
${'-'.repeat(34)}
32   Space`+f(64)

console.log(f())

Arnauld

Posted 2017-01-07T15:46:23.530

Reputation: 111 334

Nice answer! You can save one more byte by replacing \${x='Dec Chr '}| ${x}| ${x}at the end of line 2 by(x='Dec Chr ')+(' |'+x)*2+`` – Luke – 2017-01-07T18:53:12.363

@L.Serné I don't know of any version of ECMAScript that would repeat a string with the * operator. Or am I somehow misunderstanding your suggestion? – Arnauld – 2017-01-07T18:58:00.223

Oops, my bad. That's what you get for trying to golf a python solution and then javascript. – Luke – 2017-01-07T18:59:56.690

5

F#, 222 bytes

let c,p=String.concat" | ",printfn"%s"
Seq.replicate 3"Dec  Chr  "|>c|>p
p(String.replicate 34"-")
for i=32 to 63 do[for j in[i;i+32;i+64]->sprintf"%-5d%-5s"j (match j with 32->"Space"|127->"DEL"|_->string(char j))]|>c|>p

Try it online!

Asik

Posted 2017-01-07T15:46:23.530

Reputation: 161

Can I ask for an ungolfed version of this please? I'm really new to learning F#, and I'd love to understand properly how you did this! – Ciaran_McCarthy – 2018-04-25T13:24:25.740

1The first line aliases two functions to single-character names. Now if you do ["ab"; "cd"] |> c |> p it concatenates with "|" characters and prints them, like "ab | cd" which is the basic idea for printing the table. The rest is fairly straightforward, just avoiding whitespace wherever possible. – Asik – 2018-04-25T18:41:06.837

5

V, 98, 96, 94 bytes

i32 | 64 | 9631ñÙl.l.ñÍä«/&   &    
ÎéiD@"
bsDELF 27kdH5lRSpaceÄÒ-Ä3RDec  Chr³ | Î35|D

Try it online!

Just barely squeaking in under a hundred. I'm gonna see if I can beat Pyth, but I won't make any promises.

Here is a hexdump:

00000000: 6933 3220 7c20 3634 207c 2039 361b 3331  i32 | 64 | 96.31
00000010: f1d9 016c 2e6c 2ef1 cde4 ab2f 2620 2020  ...l.l...../&   
00000020: 1616 2620 2020 200a cee9 6944 4022 0a62  ..&    ...iD@".b
00000030: 7344 454c 1b46 2016 3237 6b64 4835 6c52  sDEL.F .27kdH5lR
00000040: 5370 6163 651b c4d2 2dc4 3352 4465 6320  Space...-.3RDec 
00000050: 2043 6872 b320 7c20 1bce 3335 7c44        Chr. | ..35|D

And here's how it works:

i32 | 64 | 96<esc>      " Insert some starting text
31ñ          ñ          " 31 times:
   Ù                    "   Duplicate this line
    <C-a>               "   Increment the first number on this line
         l.             "   Increment the next number
           l.           "   Increment the next number

Here is where it get's interesting. First, let me explain a vim-trick. While in insert mode, certain characters are inserted (all printable ASCII-characters, most unmapped characters above 0x7f, and a few others), but other characters have a side-effect. For example, 0x1b (<esc>) will escape to normal mode. 0x01 (<C-a>) will re-insert the last inserted text, etc. Sometimes, we want to insert these characters literally. So to insert a literal escape character, you must type <C-v><esc>. This works for all characters that have a side effect. So essentially, <C-v> is the equivalent of a backslash in languages with string literals that allow you to escape certain characters in a string.

The other useful trick with <C-v> in insert mode, is that it can be used to insert characters by code-point, in either Decimal, Hexadecimal, Octal, or Hexadecimal Unicode. Since we already have the numbers that correspond to certain ASCII values, we just need to put a <C-v> before those characters, and run the corresponding text as vim-keystrokes. This can be achieved with a regex command, and a "Do 'x' on every line" command. So we:

Í                       " Substitute globally:
 ä«                     "   One or more digits
   /                    " With:
    &                   "   The matched number + some spaces
        <C-v><C-v>&     "   A ctrl-v character, then the matched number again
                        "   Since ctrl-v is like a backslash, we need two to enter a literal ctrl-v character
Î                       " On every line:
 éi                     "   Insert an 'i'
   D                    "   Delete this line
    @"                  "   Run it as vim keystrokes

At this point, the buffer looks like this

32         | 64   @     | 96   `    
33   !     | 65   A     | 97   a    
34   "     | 66   B     | 98   b    
35   #     | 67   C     | 99   c    
36   $     | 68   D     | 100   d    
37   %     | 69   E     | 101   e    
38   &     | 70   F     | 102   f    
39   '     | 71   G     | 103   g    
40   (     | 72   H     | 104   h    
41   )     | 73   I     | 105   i    
42   *     | 74   J     | 106   j    
43   +     | 75   K     | 107   k    
44   ,     | 76   L     | 108   l    
45   -     | 77   M     | 109   m    
46   .     | 78   N     | 110   n    
47   /     | 79   O     | 111   o    
48   0     | 80   P     | 112   p    
49   1     | 81   Q     | 113   q    
50   2     | 82   R     | 114   r    
51   3     | 83   S     | 115   s    
52   4     | 84   T     | 116   t    
53   5     | 85   U     | 117   u    
54   6     | 86   V     | 118   v    
55   7     | 87   W     | 119   w    
56   8     | 88   X     | 120   x    
57   9     | 89   Y     | 121   y    
58   :     | 90   Z     | 122   z    
59   ;     | 91   [     | 123   {    
60   <     | 92   \     | 124   |    
61   =     | 93   ]     | 125   }    
62   >     | 94   ^     | 126   ~    
63   ?     | 95   _     | 127       

Now we just need some general clean up, which accounts for most of the bytes in this answer

bsDEL<esc>              " Change the literal 0x7f character to "DEL"
          F <C-v>27kd   " Remove a space from the lines that have too many
H5l                     " Move to the first space character
   RSpace<esc>          " And replace it with "Space"
Ä                       " Duplicate this line
 Ò-                     " And replace it with '-'s
   Ä                    " Duplicate this line
    3R                  " And replace it with three copies of the following string:
      Dec  Chr³ | <esc> " 'Dec  Chr   | '

Î35|D                   " Remove all but the first 35 characters of each line

James

Posted 2017-01-07T15:46:23.530

Reputation: 54 537

4

dc, 167 bytes

[[Space]nq]sp[[DEL]nq]sq[[ ]n]sc[Dec  Chr]dsen[   | ]dsfnlenlfnlen10P34[[-]n1-d0<a]dsax10P0[[32+dndZ2=c[  ]ndd32=pd127=qP[    ]n]dswx[ | ]nlwx[ | ]nlwx10P95-d32>b]dsbx

Try it online!

How it works:

[[Space]nq]sp     # p is a macro that prints "Space" and then quits from the call one level up
[[DEL]nq]sq       # q is a macro that prints "DEL" and then quits from the call one level up
[[ ]n]sc          # c is a macro that prints a space
[Dec  Chr]dsen    # Save the string "Dec  Chr" in register e, and print it.
[   | ]dsfn       # Save the string "   | " in register f, and print it.
len               # Print "Dec  Chr" again.
lfn               # Print "   | " again.
len               # Print "Dec  Chr" again.
10P               # Print a newline.
34                # Push 34 on the stack.

[[-]n1-d0<a]dsa   # a is a macro that repeatedly prints "-" and decrements the top of the stack, while the top of the stack is positive.

x10P              # Execute macro a, followed by a newline. (This prints the line of hyphens.)

0                 # Push 0 on the stack.

[                 # Starting a large macro (which will be stored in register b) for printing the table row by row.

[32+dndZ2=c[  ]ndd32=pd127=qP[    ]n]dsw

                  # w is a macro which:
                        (1) adds 32 to the top of the stack;
                        (2) prints it as a number;
                        (3) uses Z to compute the number of characters the number required to print that number;
                        (4) if it required 2 characters to print the number, calls the macro c to print an extra space
                        (5) prints the string "Space" (for ASCII code 32) or the string "DEL" (for ASCII code 127) or the appropriate character, followed by the right number of spaces

x                 # Execute macro w to print an entry in column 1.
[ | ]n            # Print a column divider.
lwx               # Execute macro w to print an entry in column 2 (ASCII code 32 higher than the previous entry).
[ | ]n            # Print a column divider.
lwx               # Execute macro w to print an entry in column 3 (ASCII code 32 higher than the previous entry).

10P               # Print a newline.
95-               # Subtract 95 to adjust to go to the beginning of the next line.

d32>b             # If the top of stack is <= 32, execute macro b again, effectively looping to print all the rows of the table.

]dsb              # End the definition of the large macro, and store it in register b.

x                 # Execute the macro that's in b (with 0 at the top of the stack initially).

Mitchell Spector

Posted 2017-01-07T15:46:23.530

Reputation: 3 392

4

Perl, 120 bytes

$,="| ";say+("Dec  Chr   ")x3;say"-"x32;say map{sprintf"%-5s%-6s",$_,$_-32?$_-127?chr:DEL:Space}$_,$_+32,$_+64for 32..63

Run with -E flag:

perl -E '$,="| ";say+("Dec  Chr   ")x3;say"-"x32;say map{sprintf"%-5s%-6s",$_,$_-32?$_-127?chr:DEL:Space}$_,$_+32,$_+64for 32..63'

-2 bytes thanks to @G B.

Dada

Posted 2017-01-07T15:46:23.530

Reputation: 8 279

If I understand a little Perl, maybe you can cut 2 spaces by using "%-5s" instead of "%-3s" (incidentally, that's what I do in ruby) – G B – 2017-01-08T13:46:46.590

3

C, 179 bytes

i;f(){for(;i++<37;)printf(i<4?"Dec  Chr%s":"-",i<3?"   | ":"\n");printf("\n32   Space | ");for(i=64;i<127;i+=i>95?-63:32)printf("%-5d%-6c%s",i,i,i>95?"\n":"| ");puts("127  DEL");}

Try it online!

Semi-ungolfed:

i;
f() {
  for(;i++<37;) printf(i<4?"Dec  Chr%s":"-",i<3?"   | ":"\n");

  printf("\n32   Space | ");
  for(i=64;i<127;i+=i>95?-63:32) printf("%-5d%-6c%s",i,i,i>95?"\n":"| ");
  puts("127  DEL");
}

simon

Posted 2017-01-07T15:46:23.530

Reputation: 351

3

V, 151 150 148 136 135 130 129 125 bytes

12 bytes saved thanks to @nmjcman101 for using <C-v>g<C-a> for the numbers instead of line('.')

2 byte saved thanks to @DJMcMayhem for removing lines with leading spaces using ÇÓ/d and by using to remove extra spaces and rearranging stuff

This answer is in competition with @nmjcman101's V answer (which uses :set ve=all). But now, I found a way to remove those A ^[s and saved some bytes and we are at an even bytecount

iSpace 
¬!~Ó./&ò
iDELí^/31   
HlgGo| 63ÙkGld/Sp
$p/`
G$d/@
$p/64
G$d/S
$pÇÓ/d
/d
hdê/32
O34é-O!| !| !Ó!/Dec  Chr   

Try it online!

Hexdump:

00000000: 6953 7061 6365 200a 1bac 217e d32e 2f26  iSpace ...!~../&
00000010: f20a 6944 454c 1bed 5e2f 3331 2020 200a  ..iDEL..^/31   .
00000020: 1648 6c67 0147 6f7c 201b 3633 d96b 1647  .Hlg.Go| .63.k.G
00000030: 6c64 2f53 700a 2470 2f60 0a16 4724 642f  ld/Sp.$p/`..G$d/
00000040: 400a 2470 2f36 340a 1647 2464 2f53 0a24  @.$p/64..G$d/S.$
00000050: 70c7 d32f 640a 2f64 0a68 64ea 2f33 320a  p../d./d.hd./32.
00000060: 4f1b 3334 e92d 4f21 7c20 217c 2021 1bd3  O.34.-O!| !| !..
00000070: 212f 4465 6320 2043 6872 2020 20         !/Dec  Chr 

Explanation (uncomplete and outdated)

The strategy here is that I'm using the line numbers to generate the ASCII code points.

Note: ^[ is 0x1b, ^V is C-v

First we generate all the characters.

iSpace             " insert Space
^[¬!~              " insert every character between ! and ~

The current buffer looks like

Space
!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~

Now we insert a newline between these characters

     Ó./&ò         " insert a newline before every character (:s/./&\r/g)

user41805

Posted 2017-01-07T15:46:23.530

Reputation: 16 320

Here's a drop in replacement for the 32 SPACE ... 127 DEL part of your code: Try it online! It uses that neat thing where you can highlight a bunch of numbers, and then g^A makes it an increasing sequence (new in Vim 8?)

– nmjcman101 – 2017-01-07T21:14:52.910

@nmjmcman101 Yeah, it was added in 7.4.something, but officially added in 8. Even better would be to use the norm command Try it online!

– James – 2017-01-07T21:22:56.280

Either way, once you get that you can do a :set ve=all and then the cursor will move into places that there is no text, letting you insert the pipes easier and copy/paste in the places you need to without sprinkling A <esc> everywhere – nmjcman101 – 2017-01-07T21:39:06.560

@nmjcman101 Re g^A thanks, it saved me 12 bytes :) – user41805 – 2017-01-08T08:06:50.597

@DJMcMayhem For some reason, using norm and then incrementing the numbers doesn't work – user41805 – 2017-01-08T08:09:44.927

You could replace those last two substitute commands with ÇÓ/d (Equivalent to :g!/\S/norm dd, e.g. delete every line not matching a non-whitespace character) – James – 2017-01-09T21:58:30.253

@DJMcMayhem Oh thanks, I didn't know there was a shortcut for the global command – user41805 – 2017-01-10T19:08:39.783

3

Ruby, 124 bytes

 puts [["Dec  Chr   "]*3*"| ",?-*34,(0..31).map{|d|(1..3).map{|x|"%-5s%-6s"%[y=x*32+d,y<33?"Space":y>126?"DEL":y.chr]}*"| "}]

G B

Posted 2017-01-07T15:46:23.530

Reputation: 11 099

3

V, 130 120 99 bytes

Sub 100 club. I'm no longer convinced that :se ve=all is the best way of doing this. It's an extra... 11 bytes just for writing the |'s! But that's what I have.

I'm posting this almost in competition with @KritixiLuthos answer using :se ve=all to avoid some A <esc>'s. I'm not convinced that either method is better yet, so hopefully this can inspire some golfing on both parties and see which method takes the cake.

I'm also half expecting @DJMcMayhem to kick both our pants

iSpace
¬!~Ó./&ò
iDELí^/31   
Hlg:se ve=all
12|êr|2ñ031j$x)PñHd)ÄÒ-Ä3RDec  Chr³ | /d
hdêÎ35|D

Try it online!

Hexdump for the curious (if there's interest I'll just change this to a vim-style hidden character block)

00000000: 6953 7061 6365 0a1b ac21 7ed3 2e2f 26f2  iSpace...!~../&.
00000010: 0a69 4445 4c1b ed5e 2f33 3120 2020 0a16  .iDEL..^/31   ..
00000020: 486c 6701 3a73 6520 7665 3d61 6c6c 0a31  Hlg.:se ve=all.1
00000030: 327c 16ea 727c 32f1 3016 3331 6a24 7829  2|..r|2.0.31j$x)
00000040: 50f1 4864 29c4 d22d c433 5244 6563 2020  P.Hd)..-.3RDec  
00000050: 4368 72b3 207c 201b 2f64 0a68 64ea ce33  Chr. | ./d.hd..3
00000060: 357c 44                                  5|D

nmjcman101

Posted 2017-01-07T15:46:23.530

Reputation: 3 274

1The global command could be shortened by quite a but. For one, the dd is implicit if you just do d. You can also do <M-s> which is always equivalent to \s in regex. So you could shorten it to: ç^ó*/d. But if you switch out the global command for the inverse :g! you could search for every line not matching a non-whitespace character. ÇÓ/d which is equivalent to :g!/\S/normal dd – James – 2017-01-11T22:59:18.183

3

AWK, 200 bytes

BEGIN{b="-----------";a="Dec  Chr   ";print a"|",a"|",a"\n-"b b b;for(a=31;a++<63;){printf"%-5d%-6s| %-5d%-6c| %-5d%-5s\n",a,a<33?"Space":sprintf("%c",a),a+32,a+32,a+64,a<63?sprintf("%c",a+64):"DEL"}}

Formatted:

BEGIN {
  b="-----------"
  a="Dec  Chr   "
  print a"|",a"|",a"\n-"b b b
  for(a=31;a++<63;) {
    printf "%-5d%-6s| %-5d%-6c| %-5d%-5s\n",
      a,    a<33 ? "Space" : sprintf("%c", a),
      a+32, a+32,
      a+64, a<63 ? sprintf("%c", a+64) : "DEL"
  }
}

Try it online!

steve

Posted 2017-01-07T15:46:23.530

Reputation: 2 276

2

Tcl, 233 bytes

lassign {"Dec  Chr" "   | " 31} h S i
set H $h$S$h$S$h\n[string repe - 34]
proc p x {format %-5d%c $x $x}
time {set H "$H
[p [incr i]]  $S[p [expr $i+32]]  $S[p [expr $i+64]]"} 32
puts [regsub {\ {8}} [regsub \177 $H DEL] "   Space"]

Try it online!

sergiol

Posted 2017-01-07T15:46:23.530

Reputation: 3 055

I ran this and I get 32 for example (a ' ' instead of Space), and 33! (no space in-between 33 and !). – NoOneIsHere – 2017-01-09T02:23:44.157

@SeeOneRhino: You are clearly not paying attention to the code. I modified the code of the link after pasting here, in attempt to golf it even more, but I did not succeed yet. I just went to page now and commented my failed attempt and repasted the code from here;If you go there now, you will see code exactly equal and you will see it print things right! – sergiol – 2017-01-09T02:56:55.733

I am sorry, I didn't see your edit to the link. I assumed that the code was the same as what you had posted here. – NoOneIsHere – 2017-01-09T02:58:58.697

No problem, it was my fault – sergiol – 2017-01-09T02:59:31.473

@SeeOneRhino: I achieved it! I outgolfed myself! – sergiol – 2017-01-10T00:07:08.903

2

C 188 Bytes

f(){i=31;printf("Dec Chr | Dec Chr | Dec Chr");printf("\n--------------------------");for(;i<63;i++)printf("\n%d%4c  | %d%4c  | %d%4c",(i+1),(i+1),(i+33),(i+33),(i+65),(i+65));puts("DEL");

Normally looks like this:

f()
{
    int  i=31;
    printf("Dec Chr | Dec Chr | Dec Chr");
    printf("\n--------------------------"); 
    for(;i<63;i++)  
      printf("\n%d%4c  | %d%4c  | %d%4c", (i+1),(i+1),(i+33),(i+33),  (i+65),(i+65));
    puts("DEL");
}

Abel Tom

Posted 2017-01-07T15:46:23.530

Reputation: 1 150

2

C (249 bytes)

Newlines added for clarity.

#define L(s,e)for(i=s;i<e;++i)
#define P printf
main(i){L(0,3)P("Dec  Chr  %s",i<2?" | ":"\n");
L(0,34)P("-");P("\n");L(32,64){P("%-5d", i);
i==32?P("Space"):P("%-5c",i);
P(" | %-5d%-5c | %-5d ",i+32,i+32,i+64);
i==63?P("DEL"):P("%-5c",i+64);P("\n");}}

musarithmia

Posted 2017-01-07T15:46:23.530

Reputation: 531

You can definitely save some bytes by making P be printf(, as shown here: https://repl.it/JBRD

– Zacharý – 2017-06-25T15:18:39.567

And by removing the space on your fourth line. – Zacharý – 2017-06-25T15:20:54.597

2

Java, 434 422 321 bytes

class A{public static void main(String[]a){
    int k=1,r,s=32;
    for(;k<4;k++)
        o("Dec   Chr  ",k);
    for(;k<37;k++)                                                                              
        o("-",k==36?3:4);
    for(k=r=s;!(k==64&&r==-63);r=k>95?-63:s,k+=r)
        o(k+"   "+((k>99)?"":" ")+(k==s?"Space":k==127?"DEL  ":((char)k+"    ")),k/s);
    }
    static void o(String s,int j){
        System.out.print(s+(j==4?"":j==3?"\n":"|"));
    }
}

Java is probably not the best language for this as there is the overhead of classes and main method...

You can eliminate main method using a static declaration, reducing the byte count down further:

class A{
    static{...}

but this results in an error (after otherwise successfully running):

Exception in thread "main" java.lang.NoSuchMethodException: A.main([Ljava.lang.String;)
    at java.lang.Class.getMethod(Class.java:1786)
    ...

The byte count does int include newlines or indentation.

xirt

Posted 2017-01-07T15:46:23.530

Reputation: 121

I don't think you need a space after void main(){, and neither between while(s.length()<5) and s+=" ". (Unless you didn't count that in your byte count). And you can save a few bytes by changing d="Dec",c="Chr" to d=p("Dec"),c=p("Chr") and making changing your calls p(d) and p(c) to d and c. – Zacharý – 2017-06-25T15:15:52.287

The 434 was removing all unnecessary whitespace. Your suggestion brings it down to 425. Thanks! – xirt – 2017-06-28T17:03:11.237

Might want to add a note about the 434 being w/o unneeded whitespace into the answer – Zacharý – 2017-06-28T17:47:44.237

Done. Note: program has significantly changed since (reduced further) so comments above may no longer be relevant – xirt – 2017-06-29T19:32:07.553

2

R, 235 228 221 212 bytes

y=apply(rbind(rep("Dec  Chr  ",3),1,matrix(sapply(1:96,function(i)paste(i+31,rep(" ",i<69),"if"(i<2,"Space","if"(i>95,"DEL",intToUtf8(c(i+31,rep(32,4))))))),nrow=32)),1,paste,collapse=" | ")
y[2]=strrep("-",34)
y

Try it online!

I tried really hard to get under 200 bytes but these paste and collapse are killing me. Returns a list of lines.

JayCe

Posted 2017-01-07T15:46:23.530

Reputation: 2 655

1

Python 2, 1564 218 bytes

My first golf, sorry for obvious mistakes

print("Dec  Chr   | "*3)[:-2]+"\n"+"-"*34+"\n32   Space | 64   @     | 96   `"
for n in range(33,63):print"| ".join([str(n+x).ljust(5)+chr(n+x).ljust(6)for x in [0,32,64]])
print"63   ?     | 95   _     | 127  DEL"

Try it online!

Incase you're wondering, the first version was a base64 encoded string.

sagiksp

Posted 2017-01-07T15:46:23.530

Reputation: 1 249

@FlipTack Changed it to an actual solution – sagiksp – 2017-01-07T19:42:43.780

Unrequired whitespace at ljust(6) for. – Yytsi – 2017-01-13T17:28:19.933

Another at x in [. And IIRC the square brackets inside join can deleted. – Yytsi – 2017-01-14T08:29:53.023

1

Perl, 165 155 bytes

$s='Dec  Chr   ';$_=join"\n",("$s| $s| $s","-"x34,map{join"| ",map{sprintf'%1$-5d%1$-6c',$_}($_,$_+32,$_+64)}32..63);s/ {8}/   Space/;s/\x7f.*/DEL\n/;print

nwk

Posted 2017-01-07T15:46:23.530

Reputation: 621

1

JavaScript (ES6), 258 bytes

a="Dec  Chr   | ".repeat(2)+"Dec  Chr\n"+"-".repeat(34);for(b=32;64>b;b++)a+="\n"+b+"   "+(32==b?"Space  ":String.fromCharCode(b)+"      ")+"| "+(b+32)+"   "+String.fromCharCode(b+32)+"     | "+(b+64)+(35<b?"  ":"   ")+(63==b?"DEL":String.fromCharCode(b+64))

console.log(a)

Julian Lachniet

Posted 2017-01-07T15:46:23.530

Reputation: 3 216

1

PowerShell, 159 bytes

,'Dec  Chr'*3-join'   | '
'-'*34
32..63|%{($_,($_+32),($_+64)|%{"$_".PadRight(5)+"$(([char]$_,('Space','DEL')[$_-ne32])[$_-in32,127])".padRight(5)})-join' | '}

Try it online!

The first two lines are just creating literal strings and leaving them on the pipeline. The first uses the comma operator , to create an array, and then -joins that array together to create the headers. The second is just a straight string multiplication.

The third line loops over 32..63 and each iteration sends three values $_, ($_+32), and ($_+64) into an inner loop. The inner loop does a PadRight on the value (adds the appropriate spaces to pad to 5 characters). That is then string concatenated + with the result of a nested pseudo-ternary ( )[ ]. The pseudo-ternary selects either the char representation of that number, or else Space or DEL if it's the appropriate value. Again, we PadRight the appropriate characters.

Those three strings (for example, 32 Space, 64 @, 96 `) are encapsulated in parens and -joined with the column markers into a single string. Each of those 32 strings are then left on the pipeline. At the end of execution, an implicit Write-Output inserts a newline between elements on the pipeline, so we get that for free.

AdmBorkBork

Posted 2017-01-07T15:46:23.530

Reputation: 41 581

1

PHP, 163 149 147 bytes

<?=($p=str_pad)(D,31,"ec Chr   | D"),$p("
",32,"-");whhile($i<96)printf("%s%-4d%-6s",$i%3?"| ":"
",$o=$i%3*32+32+$i/3,$i++?$i<96?chr($o):DEL:Space);

breakdown

                        # print header
<?=($p=str_pad)(D,31,"ec Chr   | D"),$p("\n",32,"-");
while($i<96)            # loop $i from 0 to 96
    printf("%s%-4d%-6s",    # print formatted:
                            # string, 4 space decimal leftbound, 6 space string leftbound
        $i%3?"| ":"\n",                 # linebreak for 1st column, pipe+space else
        $o=$i%3*32+32+$i/3,             # ($i mapped to) ASCII value
        $i++?$i<96?chr($o):DEL:Space    # character
    );

Using %-N is worth the byte that rightbound numbers and character would save.

Titus

Posted 2017-01-07T15:46:23.530

Reputation: 13 814

Looks better in my opinion with the same byte count Try it online!

– Jörg Hülsermann – 2017-06-25T15:21:12.937

1

05AB1E, 82 76 bytes

žQSDÇƵQ¸«.Bs𔇲”:"DEL"¸«.B)øvyð2×ý})3äøvy… | ©ý}®”†…  Chr  ÿ”3ר¨'-34×.Á.Á»

Try it online!

Still golfing, this can be improved a lot.


žQSDÇƵQ¸«.Bs𔇲”:"DEL"¸«.B)ø pushes padded numbers with text equivalent:

[['32 ', 'Space'], ['33 ', '!    '], ['34 ', '"    '], ['35 ', '#    '], ['36 ', '$    '], ['37 ', '%    '], ['38 ', '&    '], ['39 ', "'    "], ['40 ', '(    '], ['41 ', ')    '], ['42 ', '*    '], ['43 ', '+    '], ['44 ', ',    '], ['45 ', '-    '], ['46 ', '.    '], ['47 ', '/    '], ['48 ', '0    '], ['49 ', '1    '], ['50 ', '2    '], ['51 ', '3    '], ['52 ', '4    '], ['53 ', '5    '], ['54 ', '6    '], ['55 ', '7    '], ['56 ', '8    '], ['57 ', '9    '], ['58 ', ':    '], ['59 ', ';    '], ['60 ', '<    '], ['61 ', '=    '], ['62 ', '>    '], ['63 ', '?    '], ['64 ', '@    '], ['65 ', 'A    '], ['66 ', 'B    '], ['67 ', 'C    '], ['68 ', 'D    '], ['69 ', 'E    '], ['70 ', 'F    '], ['71 ', 'G    '], ['72 ', 'H    '], ['73 ', 'I    '], ['74 ', 'J    '], ['75 ', 'K    '], ['76 ', 'L    '], ['77 ', 'M    '], ['78 ', 'N    '], ['79 ', 'O    '], ['80 ', 'P    '], ['81 ', 'Q    '], ['82 ', 'R    '], ['83 ', 'S    '], ['84 ', 'T    '], ['85 ', 'U    '], ['86 ', 'V    '], ['87 ', 'W    '], ['88 ', 'X    '], ['89 ', 'Y    '], ['90 ', 'Z    '], ['91 ', '[    '], ['92 ', '\\    '], ['93 ', ']    '], ['94 ', '^    '], ['95 ', '_    '], ['96 ', '`    '], ['97 ', 'a    '], ['98 ', 'b    '], ['99 ', 'c    '], ['100', 'd    '], ['101', 'e    '], ['102', 'f    '], ['103', 'g    '], ['104', 'h    '], ['105', 'i    '], ['106', 'j    '], ['107', 'k    '], ['108', 'l    '], ['109', 'm    '], ['110', 'n    '], ['111', 'o    '], ['112', 'p    '], ['113', 'q    '], ['114', 'r    '], ['115', 's    '], ['116', 't    '], ['117', 'u    '], ['118', 'v    '], ['119', 'w    '], ['120', 'x    '], ['121', 'y    '], ['122', 'z    '], ['123', '{    '], ['124', '|    '], ['125', '}    '], ['126', '~    '], ['127', 'DEL  ']]

vyð2×ý})3äøvy… | ©ý} joins 'em together into the table:

['32   Space | 64   @     | 96   `    ', '33   !     | 65   A     | 97   a    ', '34   "     | 66   B     | 98   b    ', '35   #     | 67   C     | 99   c    ', '36   $     | 68   D     | 100  d    ', '37   %     | 69   E     | 101  e    ', '38   &     | 70   F     | 102  f    ', "39   '     | 71   G     | 103  g    ", '40   (     | 72   H     | 104  h    ', '41   )     | 73   I     | 105  i    ', '42   *     | 74   J     | 106  j    ', '43   +     | 75   K     | 107  k    ', '44   ,     | 76   L     | 108  l    ', '45   -     | 77   M     | 109  m    ', '46   .     | 78   N     | 110  n    ', '47   /     | 79   O     | 111  o    ', '48   0     | 80   P     | 112  p    ', '49   1     | 81   Q     | 113  q    ', '50   2     | 82   R     | 114  r    ', '51   3     | 83   S     | 115  s    ', '52   4     | 84   T     | 116  t    ', '53   5     | 85   U     | 117  u    ', '54   6     | 86   V     | 118  v    ', '55   7     | 87   W     | 119  w    ', '56   8     | 88   X     | 120  x    ', '57   9     | 89   Y     | 121  y    ', '58   :     | 90   Z     | 122  z    ', '59   ;     | 91   [     | 123  {    ', '60   <     | 92   \\     | 124  |    ', '61   =     | 93   ]     | 125  }    ', '62   >     | 94   ^     | 126  ~    ', '63   ?     | 95   _     | 127  DEL  ']

®”†… Chr ÿ”3ר¨'-34×.Á.Á» takes care of the header portion of the table:

Dec  Chr   | Dec  Chr   | Dec  Chr   
----------------------------------
32   Space | 64   @     | 96   `    
33   !     | 65   A     | 97   a    
34   "     | 66   B     | 98   b    
35   #     | 67   C     | 99   c    
36   $     | 68   D     | 100  d    
37   %     | 69   E     | 101  e    
38   &     | 70   F     | 102  f    
39   '     | 71   G     | 103  g    
40   (     | 72   H     | 104  h    
41   )     | 73   I     | 105  i    
42   *     | 74   J     | 106  j    
43   +     | 75   K     | 107  k    
44   ,     | 76   L     | 108  l    
45   -     | 77   M     | 109  m    
46   .     | 78   N     | 110  n    
47   /     | 79   O     | 111  o    
48   0     | 80   P     | 112  p    
49   1     | 81   Q     | 113  q    
50   2     | 82   R     | 114  r    
51   3     | 83   S     | 115  s    
52   4     | 84   T     | 116  t    
53   5     | 85   U     | 117  u    
54   6     | 86   V     | 118  v    
55   7     | 87   W     | 119  w    
56   8     | 88   X     | 120  x    
57   9     | 89   Y     | 121  y    
58   :     | 90   Z     | 122  z    
59   ;     | 91   [     | 123  {    
60   <     | 92   \     | 124  |    
61   =     | 93   ]     | 125  }    
62   >     | 94   ^     | 126  ~    
63   ?     | 95   _     | 127  DEL  

Magic Octopus Urn

Posted 2017-01-07T15:46:23.530

Reputation: 19 422

1

Stax, 53 bytes

éV ►Γ┐º╬¼1N_◄╒0└♀α→┴♂┐Y♣╟n»┤3°k⌡{═╥I⌂╜<─W}íè7Y♠Σ▲ÿb#√

Run and debug it

recursive

Posted 2017-01-07T15:46:23.530

Reputation: 8 616

0

T-SQL, 242 bytes

DECLARE @ INT=32PRINT'Dec  Chr   | Dec  Chr   | Dec  Chr
'+REPLICATE('-',34)L:PRINT CONCAT(@,'   ',IIF(@=32,'Space | ',CHAR(@)+'     | '),@+32,'   ',CHAR(@+32),'     | ',@+64,SPACE(5-LEN(@+64)),IIF(@=63,'DEL',CHAR(@+64)))SET @+=1IF @<64GOTO L

Formatted:

DECLARE @ INT=32
PRINT'Dec  Chr   | Dec  Chr   | Dec  Chr
' + REPLICATE('-',34)
L:
    PRINT CONCAT(@,'   ',IIF(@=32,'Space | ',CHAR(@)+'     | ')
                ,@+32,'   ',CHAR(@+32),'     | ',
                 @+64,SPACE(5-LEN(@+64)),IIF(@=63,'DEL',CHAR(@+64)))
    SET @+=1
IF @<64 GOTO L

Tried a few variations, including replacing various common strings with other variables, but this was the shortest version I found.

BradC

Posted 2017-01-07T15:46:23.530

Reputation: 6 099

0

Python 3, 154 bytes

for l in[['Dec  Chr  ']*3,['-'*35]]+[[f"{x:<5}{['Space',chr(x),'DEL'][(x>32)+(x>126)]:5}"for x in(c,c+32,c+64)]for c in range(32,64)]:print(' | '.join(l))

Try it online!

ovs

Posted 2017-01-07T15:46:23.530

Reputation: 21 408

0

Attache, 144 bytes

(p:=PadRight)["",37,"Dec  Chr   | "]'(34*"-")'(Join&"| "=>Tr@ChopInto&3<|{p&11<|p&5@Repr@_+(<~32->$Space,127->$DEL~>@_or Char!_)}=>32:127)|Print

Try it online!

Conor O'Brien

Posted 2017-01-07T15:46:23.530

Reputation: 36 228