TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to SWIFT

Swift ASCII Table

Handle ASCII characters and print an ASCII table. Use UnicodeScalar and escaped.
ASCII table. In ASCII, characters all have underlying integer values of 0 through 127 inclusive. Characters are numbers. We can use Swift to print an ASCII table.
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 table: # 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.

For: We use a for-loop with an open-ended range. So we loop from 1 to 127 inclusively.

For

UnicodeScalar: We convert each int into a UnicodeScalar. We use escaped() to get an escaped representation of each value.

Padding: We use padding() with toLength to make our output table look better. This step is optional.

Padding
Swift program that generates ASCII table import Foundation // Header. print("# ASCII") // Use values between 0 and 127. let min = 0 let max = 128 // Loop over all possible indexes. for var i in min..<max { // Get UnicodeScalar. let u = UnicodeScalar(i)! // Build left part of display line. var displayIndex = String(i).padding(toLength: 5, withPad: " ", startingAt: 0) // Escape the UnicodeScalar for display. var display = u.escaped(asASCII: true) // Print this line. var 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.
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 to Character
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.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf