Show ASCII table
- Task
Show ASCII character set from values 32 to 127 in table format.
C
<lang c>#include <stdio.h>
int main() {
int i, j; char k[4]; for (i = 0; i < 16; ++i) { for (j = 32 + i; j < 128; j += 16) { switch (j) { default: sprintf(k, "%c", j); break; case 32: sprintf(k, "Spc"); break; case 127: sprintf(k, "Del"); break; } printf("%3d : %-3s ", j, k); } printf("\n"); } return 0;
}</lang>
- Output:
32 : Spc 48 : 0 64 : @ 80 : P 96 : ` 112 : p 33 : ! 49 : 1 65 : A 81 : Q 97 : a 113 : q 34 : " 50 : 2 66 : B 82 : R 98 : b 114 : r 35 : # 51 : 3 67 : C 83 : S 99 : c 115 : s 36 : $ 52 : 4 68 : D 84 : T 100 : d 116 : t 37 : % 53 : 5 69 : E 85 : U 101 : e 117 : u 38 : & 54 : 6 70 : F 86 : V 102 : f 118 : v 39 : ' 55 : 7 71 : G 87 : W 103 : g 119 : w 40 : ( 56 : 8 72 : H 88 : X 104 : h 120 : x 41 : ) 57 : 9 73 : I 89 : Y 105 : i 121 : y 42 : * 58 : : 74 : J 90 : Z 106 : j 122 : z 43 : + 59 : ; 75 : K 91 : [ 107 : k 123 : { 44 : , 60 : < 76 : L 92 : \ 108 : l 124 : | 45 : - 61 : = 77 : M 93 : ] 109 : m 125 : } 46 : . 62 : > 78 : N 94 : ^ 110 : n 126 : ~ 47 : / 63 : ? 79 : O 95 : _ 111 : o 127 : Del
Factor
Idiomatic version
<lang factor>USING: combinators formatting io kernel math math.ranges pair-rocket sequences ; IN: rosetta-code.ascii-table
- row-values ( n -- seq ) [ 32 + ] [ 112 + ] bi 16 <range> ;
- ascii>output ( n -- str )
{ 32 => [ "Spc" ] 127 => [ "Del" ] [ "" 1sequence ] } case ;
- print-row ( n -- )
row-values [ dup ascii>output "%3d : %-3s " printf ] each nl ;
- print-ascii-table ( -- ) 16 <iota> [ print-row ] each ;
MAIN: print-ascii-table</lang>
Go translation
<lang factor>USING: combinators formatting io kernel math math.ranges pair-rocket sequences ; IN: rosetta-code.ascii-table
- main ( -- )
16 <iota> [ 32 + 127 16 <range> [ dup { 32 => [ "Spc" ] 127 => [ "Del" ] [ "" 1sequence ] } case "%3d : %-3s " printf ] each nl ] each
MAIN: main</lang>
- Output:
32 : Spc 48 : 0 64 : @ 80 : P 96 : ` 112 : p 33 : ! 49 : 1 65 : A 81 : Q 97 : a 113 : q 34 : " 50 : 2 66 : B 82 : R 98 : b 114 : r 35 : # 51 : 3 67 : C 83 : S 99 : c 115 : s 36 : $ 52 : 4 68 : D 84 : T 100 : d 116 : t 37 : % 53 : 5 69 : E 85 : U 101 : e 117 : u 38 : & 54 : 6 70 : F 86 : V 102 : f 118 : v 39 : ' 55 : 7 71 : G 87 : W 103 : g 119 : w 40 : ( 56 : 8 72 : H 88 : X 104 : h 120 : x 41 : ) 57 : 9 73 : I 89 : Y 105 : i 121 : y 42 : * 58 : : 74 : J 90 : Z 106 : j 122 : z 43 : + 59 : ; 75 : K 91 : [ 107 : k 123 : { 44 : , 60 : < 76 : L 92 : \ 108 : l 124 : | 45 : - 61 : = 77 : M 93 : ] 109 : m 125 : } 46 : . 62 : > 78 : N 94 : ^ 110 : n 126 : ~ 47 : / 63 : ? 79 : O 95 : _ 111 : o 127 : Del
Go
<lang go>package main
import "fmt"
func main() {
for i := 0; i < 16; i++ { for j := 32 + i; j < 128; j += 16 { k := string(j) switch j { case 32: k = "Spc" case 127: k = "Del" } fmt.Printf("%3d : %-3s ", j, k) } fmt.Println() }
} </lang>
- Output:
32 : Spc 48 : 0 64 : @ 80 : P 96 : ` 112 : p 33 : ! 49 : 1 65 : A 81 : Q 97 : a 113 : q 34 : " 50 : 2 66 : B 82 : R 98 : b 114 : r 35 : # 51 : 3 67 : C 83 : S 99 : c 115 : s 36 : $ 52 : 4 68 : D 84 : T 100 : d 116 : t 37 : % 53 : 5 69 : E 85 : U 101 : e 117 : u 38 : & 54 : 6 70 : F 86 : V 102 : f 118 : v 39 : ' 55 : 7 71 : G 87 : W 103 : g 119 : w 40 : ( 56 : 8 72 : H 88 : X 104 : h 120 : x 41 : ) 57 : 9 73 : I 89 : Y 105 : i 121 : y 42 : * 58 : : 74 : J 90 : Z 106 : j 122 : z 43 : + 59 : ; 75 : K 91 : [ 107 : k 123 : { 44 : , 60 : < 76 : L 92 : \ 108 : l 124 : | 45 : - 61 : = 77 : M 93 : ] 109 : m 125 : } 46 : . 62 : > 78 : N 94 : ^ 110 : n 126 : ~ 47 : / 63 : ? 79 : O 95 : _ 111 : o 127 : Del
Kotlin
<lang scala>// Version 1.2.60
fun main(args: Array<String>) {
for (i in 0..15) { for (j in 32 + i..127 step 16) { val k = when (j) { 32 -> "Spc" 127 -> "Del" else -> j.toChar().toString() } System.out.printf("%3d : %-3s ", j, k) } println() }
}</lang>
- Output:
32 : Spc 48 : 0 64 : @ 80 : P 96 : ` 112 : p 33 : ! 49 : 1 65 : A 81 : Q 97 : a 113 : q 34 : " 50 : 2 66 : B 82 : R 98 : b 114 : r 35 : # 51 : 3 67 : C 83 : S 99 : c 115 : s 36 : $ 52 : 4 68 : D 84 : T 100 : d 116 : t 37 : % 53 : 5 69 : E 85 : U 101 : e 117 : u 38 : & 54 : 6 70 : F 86 : V 102 : f 118 : v 39 : ' 55 : 7 71 : G 87 : W 103 : g 119 : w 40 : ( 56 : 8 72 : H 88 : X 104 : h 120 : x 41 : ) 57 : 9 73 : I 89 : Y 105 : i 121 : y 42 : * 58 : : 74 : J 90 : Z 106 : j 122 : z 43 : + 59 : ; 75 : K 91 : [ 107 : k 123 : { 44 : , 60 : < 76 : L 92 : \ 108 : l 124 : | 45 : - 61 : = 77 : M 93 : ] 109 : m 125 : } 46 : . 62 : > 78 : N 94 : ^ 110 : n 126 : ~ 47 : / 63 : ? 79 : O 95 : _ 111 : o 127 : Del
Perl 6
Alternately, and perhaps more usefully, output as a wiki-table rather than ASCII art. Hover mouse over the glyph to get the name.
<lang perl6>sub name ($_) {
when * < 33 { (0x2400 + $_).chr } # display symbol names for invisible glyphs when 127 { '␡' } default { .chr }
}
say '{|class="wikitable" style="text-align:center;background-color:hsl(39, 90%, 95%)"';
for (^128).rotor(16) -> @row {
say '|-'; printf(q[|%d
0x%02X
%s] ~ "\n", $_, $_, .&name.uniname.subst('SYMBOL FOR ', ), .&name.subst('|', '|')) for @row;
}
say '|}';</lang>
- Output:
0 0x00 ␀ |
1 0x01 ␁ |
2 0x02 ␂ |
3 0x03 ␃ |
4 0x04 ␄ |
5 0x05 ␅ |
6 0x06 ␆ |
7 0x07 ␇ |
8 0x08 ␈ |
9 0x09 ␉ |
10 0x0A ␊ |
11 0x0B ␋ |
12 0x0C ␌ |
13 0x0D ␍ |
14 0x0E ␎ |
15 0x0F ␏ |
16 0x10 ␐ |
17 0x11 ␑ |
18 0x12 ␒ |
19 0x13 ␓ |
20 0x14 ␔ |
21 0x15 ␕ |
22 0x16 ␖ |
23 0x17 ␗ |
24 0x18 ␘ |
25 0x19 ␙ |
26 0x1A ␚ |
27 0x1B ␛ |
28 0x1C ␜ |
29 0x1D ␝ |
30 0x1E ␞ |
31 0x1F ␟ |
32 0x20 ␠ |
33 0x21 ! |
34 0x22 " |
35 0x23 # |
36 0x24 $ |
37 0x25 % |
38 0x26 & |
39 0x27 ' |
40 0x28 ( |
41 0x29 ) |
42 0x2A * |
43 0x2B + |
44 0x2C , |
45 0x2D - |
46 0x2E . |
47 0x2F / |
48 0x30 0 |
49 0x31 1 |
50 0x32 2 |
51 0x33 3 |
52 0x34 4 |
53 0x35 5 |
54 0x36 6 |
55 0x37 7 |
56 0x38 8 |
57 0x39 9 |
58 0x3A : |
59 0x3B ; |
60 0x3C < |
61 0x3D = |
62 0x3E > |
63 0x3F ? |
64 0x40 @ |
65 0x41 A |
66 0x42 B |
67 0x43 C |
68 0x44 D |
69 0x45 E |
70 0x46 F |
71 0x47 G |
72 0x48 H |
73 0x49 I |
74 0x4A J |
75 0x4B K |
76 0x4C L |
77 0x4D M |
78 0x4E N |
79 0x4F O |
80 0x50 P |
81 0x51 Q |
82 0x52 R |
83 0x53 S |
84 0x54 T |
85 0x55 U |
86 0x56 V |
87 0x57 W |
88 0x58 X |
89 0x59 Y |
90 0x5A Z |
91 0x5B [ |
92 0x5C \ |
93 0x5D ] |
94 0x5E ^ |
95 0x5F _ |
96 0x60 ` |
97 0x61 a |
98 0x62 b |
99 0x63 c |
100 0x64 d |
101 0x65 e |
102 0x66 f |
103 0x67 g |
104 0x68 h |
105 0x69 i |
106 0x6A j |
107 0x6B k |
108 0x6C l |
109 0x6D m |
110 0x6E n |
111 0x6F o |
112 0x70 p |
113 0x71 q |
114 0x72 r |
115 0x73 s |
116 0x74 t |
117 0x75 u |
118 0x76 v |
119 0x77 w |
120 0x78 x |
121 0x79 y |
122 0x7A z |
123 0x7B { |
124 0x7C | |
125 0x7D } |
126 0x7E ~ |
127 0x7F ␡ |
Ring
<lang ring>
- Project : Show Ascii table
load "guilib.ring" load "stdlib.ring"
decarr = newlist(16,6) ascarr = newlist(16,6)
new qapp
{ win1 = new qwidget() { setwindowtitle("Show Ascii table") setgeometry(100,100,800,600) for n = 1 to 16 for m = 1 to 6 decarr[n][m] = new qpushbutton(win1) { x = 150+m*60 y = 30 + n*30 ind = string((m-1)*16+n+31) setgeometry(x,y,30,30) settext(ind) } next next for n = 1 to 16 for m = 1 to 6 ascarr[n][m] = new qpushbutton(win1) { x = 180+m*60 y = 30 + n*30 ind = (m-1)*16+n+31 setgeometry(x,y,30,30) if ind = 32 settext("Spc") loop ok if ind = 127 settext("Del") loop ok settext(char(ind)) } next next show() } exec() }
</lang> Output: