Recreate the ASCII-table as an ASCII-table

  • 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!


    I have that exact ASCII table image stuck on my wall... Anyway, can a list of lines be returned?

    @fliptack Of course you can. Why wouldn't I allow something perfectly reasonable like that?

    I 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 It still has Space and DEL

    @gurka yes, but the other one has every single control character.

  • Python 2 3.6, 185 183 175 159 156 bytes



    Saved 8 bytes thanks to FlipTack!



    Still quite new to golfing in Python.



    for a in["Dec  Chr   | "*3,"-"*39]+["".join(f"{l:<5}{('Space',chr(l),'DEL')[(l>32)+(l>126)]:<6}| "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
    f"{l:<5}{('Space',chr(l),'DEL')[(l>32)+(l>126)]:<6}| "
    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]


    Update: apparently using f-string here is shorter than the % operator.






    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]

    I see a space at ` for l in(i,32+i,64+i)])`, could be removed to save one byte.

  • 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!


  • Pyth, 89 85 79 77 bytes



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


    Try it online!


  • 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())




    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+\``

    @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?

    Oops, my bad. That's what you get for trying to golf a python solution and then javascript.

  • 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!


    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!

    The 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.

  • V, 98, 96, 94 bytes



    i32 | 64 | 9631ñÙl.l.ñÍä«/&   &    
    Îé[email protected]"
    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 ..& [email protected]".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

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

  • 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.


    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)

  • 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

    The global command could be shortened by quite a but. For one, the `dd` is implicit if you just do `d`. You can also do `` 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`

  • 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!


License under CC-BY-SA with attribution


Content dated before 7/24/2021 11:53 AM