Home
Swift
ASCII Table
Updated Nov 15, 2023
Dot Net Perls
ASCII table. In ASCII, characters all have underlying integer values of 0 through 127 inclusive. Characters are numbers. We can use Swift 5.8 to print an ASCII table.
With a for-loop, and UnicodeScalar, we can generate a simple ASCII table in Swift. Another tutorial converts characters and Ints. This helps when handling ASCII.
Convert Int, Character
On UnicodeScalar, we can call the escaped() method to display ASCII characters on the console. This can help with debugging—output is easier to read.
# ASCII 0 \0 1 \u{01} 2 \u{02} 3 \u{03} 4 \u{04} 5 \u{05} 6 \u{06} 7 \u{07} 8 \u{08} 9 \t 10 \n 11 \u{0B} 12 \u{0C} 13 \r 14 \u{0E} 15 \u{0F} 16 \u{10} 17 \u{11} 18 \u{12} 19 \u{13} 20 \u{14} 21 \u{15} 22 \u{16} 23 \u{17} 24 \u{18} 25 \u{19} 26 \u{1A} 27 \u{1B} 28 \u{1C} 29 \u{1D} 30 \u{1E} 31 \u{1F} 32 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 \u{7F}
Swift program. Above we see the ASCII table generated by this Swift program. Some of the ASCII characters, like control and whitespace characters, are escaped.
Start We use a for-loop with an open-ended range. So we loop from 1 to 127 inclusively.
for
Next We convert each int into a UnicodeScalar. We use escaped() to get an escaped representation of each value.
Then We use padding() with toLength to make our output table look better. This step is optional.
String padding
import Foundation // Header. print("# ASCII") // Use values between 0 and 127. let min = 0 let max = 128 // Loop over all possible indexes. for i in min..<max { // Get UnicodeScalar. let u = UnicodeScalar(i)! // Build left part of display line. let displayIndex = String(i).padding(toLength: 5, withPad: " ", startingAt: 0) // Escape the UnicodeScalar for display. let display = u.escaped(asASCII: true) // Print this line. let result = "\(displayIndex) \(display)" print(result) }
Some notes. This ASCII table is not perfect. The escaped() method on UnicodeScalar escapes all quotes, and this may not be needed.
A review. In all general programming languages, we can generate ASCII tables. This helps us understand how characters are handled—in Swift we use the UnicodeScalar type.
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Nov 15, 2023 (edit).
Home
Changes
© 2007-2025 Sam Allen