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
Haskell
<lang haskell>import Data.List (intercalate, transpose) import Data.Char (chr)
asciiTable :: String asciiTable =
unlines $ (justifyLeft 12 ' ' =<<) <$> transpose (chunksOf 16 $ asciiEntry <$> [32 .. 127])
main :: IO () main = putStrLn asciiTable
asciiName :: Int -> String asciiName n
| 32 > n = "" | 127 < n = "" | 32 == n = "Spc" | 127 == n = "Del" | otherwise = [chr n]
asciiEntry :: Int -> String asciiEntry n =
let k = asciiName n in if null k then "" else concat [justifyRight 4 ' ' (show n), " : ", k]
chunksOf :: Int -> [a] -> a chunksOf k = go
where go t = case splitAt k t of (a, b) | null a -> [] | otherwise -> a : go b
justifyLeft, justifyRight :: Int -> Char -> String -> String justifyLeft n c s = take n (s ++ replicate n c)
justifyRight n c s = drop (length s) (replicate n c ++ s)</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
JavaScript
<lang javascript>(() => {
// asciiTable :: String const asciiTable = () => unlines( map(xs => concat( map(justifyLeft(12, ' '), xs ) ), transpose( chunksOf( 16, map(asciiEntry, enumFromTo(32, 127) ) ) ) ) );
// asciiEntry :: Int -> String const asciiEntry = n => { const k = asciiName(n); return === k ? ( ) : (justifyRight(4, ' ', n.toString()) + ' : ' + k); };
// asciiName :: Int -> String const asciiName = n => 32 > n || 127 < n ? ( ) : 32 === n ? ( 'Spc' ) : 127 === n ? ( 'Del' ) : chr(n);
// GENERIC FUNCTIONS ----------------------------------
// chunksOf :: Int -> [a] -> a const chunksOf = (n, xs) => xs.reduce((a, _, i, xs) => i % n ? a : a.concat([xs.slice(i, i + n)]), []);
// chr :: Int -> Char const chr = String.fromCodePoint;
// comparing :: (a -> b) -> (a -> a -> Ordering) const comparing = f => (x, y) => { const a = f(x), b = f(y); return a < b ? -1 : (a > b ? 1 : 0); };
// concat :: a -> [a] // concat :: [String] -> String const concat = xs => 0 < xs.length ? (() => { const unit = 'string' !== typeof xs[0] ? ( [] ) : ; return unit.concat.apply(unit, xs); })() : [];
// concatMap :: (a -> [b]) -> [a] -> [b] const concatMap = (f, xs) => 0 < xs.length ? (() => { const unit = 'string' !== typeof xs ? ( [] ) : ; return unit.concat.apply(unit, xs.map(f)) })() : [];
// enumFromTo :: Int -> Int -> [Int] const enumFromTo = (m, n) => m <= n ? iterateUntil( x => n <= x, x => 1 + x, m ) : [];
// iterateUntil :: (a -> Bool) -> (a -> a) -> a -> [a] const iterateUntil = (p, f, x) => { const vs = [x]; let h = x; while (!p(h))(h = f(h), vs.push(h)); return vs; };
// justifyLeft :: Int -> Char -> String -> String const justifyLeft = (n, cFiller) => strText => n > strText.length ? ( (strText + cFiller.repeat(n)) .substr(0, n) ) : strText;
// justifyRight :: Int -> Char -> String -> String const justifyRight = (n, cFiller, strText) => n > strText.length ? ( (cFiller.repeat(n) + strText) .slice(-n) ) : strText;
// length :: [a] -> Int const length = xs => xs.length;
// map :: (a -> b) -> [a] -> [b] const map = (f, xs) => xs.map(f);
// maximumBy :: (a -> a -> Ordering) -> [a] -> a const maximumBy = (f, xs) => 0 < xs.length ? ( xs.slice(1) .reduce((a, x) => 0 < f(x, a) ? x : a, xs[0]) ) : undefined;
// replicate :: Int -> a -> [a] const replicate = (n, x) => Array.from({ length: n }, () => x);
// transpose :: a -> a const transpose = tbl => { const gaps = replicate( length(maximumBy(comparing(length), tbl)), [] ), rows = map(xs => xs.concat(gaps.slice(xs.length)), tbl); return map( (_, col) => concatMap(row => [row[col]], rows), rows[0] ); };
// unlines :: [String] -> String const unlines = xs => xs.join('\n');
// MAIN ----------------------------------------------- return asciiTable();
})();</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 glyph ($_) {
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", $_, $_, .&glyph.uniname.subst('SYMBOL FOR ', ), .&glyph.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 ␡ |
Python
After Perl6, but creating an HTML table: <lang python>from unicodedata import name from html import escape
def pp(n):
if n <= 32: return chr(0x2400 + n) if n == 127: return '␡' return chr(n)
print('
\n ') for n in range(128): if n %16 == 0 and 1 < n: print(" ") print(f' ') print(""" \n{n} 0x{n:02x} {escape(pp(n))} |
""")</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:
zkl
<lang zkl>const width=9; println(" ",[0..width].pump(String,"%4d".fmt)); [30..127].pump("".println,T(Void.Read,width,False), // don't fail on short lines
fcn(a,b,c){ String("%3d: ".fmt(a), vm.arglist.pump(String,"toChar", // parameters (ints) to list to text T("replace","\x1e",""),T("replace","\x1f",""), // 30,31 T("replace"," ","spc"),T("replace","\x7f","del"), "%-4s".fmt) ) })</lang>
- Output:
0 1 2 3 4 5 6 7 8 9 30: spc ! " # $ % & ' 40: ( ) * + , - . / 0 1 50: 2 3 4 5 6 7 8 9 : ; 60: < = > ? @ A B C D E 70: F G H I J K L M N O 80: P Q R S T U V W X Y 90: Z [ \ ] ^ _ ` a b c 100: d e f g h i j k l m 110: n o p q r s t u v w 120: x y z { | } ~ del