- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Swift Program to Print the ASCII values
This tutorial will discuss how to write a Swift program to print the ASCII values.
ASCII is known as American Standard Code for Information Interchange. In electronic communication, it is a character encoding standard to represent text in the computer and other devices. It generally has 128 standard ASCII codes including every upper case, lowercase alphabets, digits starting from 0-9 and symbols, etc., and assign each character a unique code.
Below is a demonstration of the same −
Suppose we enter the following input −
Value = A
Following is the desired output −
ASCII value is 65
We can find ASCII value using the following methods −
ASCII value using utf8 property
We can find the ASCII value using utf8 property. This property converts the given string into its UTF-8 code. Or we can say that utf8 property encodes the string’s Unicode scalar values into 8-bit integer value.
Syntax
Following is the syntax of utf8 property −
mystring.utf8
Algorithm
The algorithm is explained below −
Step 1 − Declare a variable with value - val = abc
Step 2 − Run a for loop with val.utf8.
Step 3 − Print the output
Example
The following program shows how to print the ASCII values using utf8 property.
import Foundation import Glibc var val = "abc" print("ASCII values are:") for k in val.utf8{ print(k) }
Output
ASCII values are: 97 98 99
Here, in the above code, we have a string of characters that is val = “abc”. Now use val.utf8 property in for loop to find the ASCII value of each character like the ASCII value of a = 97, b = 98, c = 99.
ASCII value using value property
We can also find the ASCII value of the given character with the help of value property of UnicodeScalar() function. The value property is used to find the numeric value of the given Unicode Scalar.
Syntax
Following is the syntax of value property −
UnicodeScalar(“a”).value
Algorithm
The algorithm is explained below −
Step 1 − Declare a variable with unicode value- val1 = UnicodeScalar("a").value
Step 2 − Print the output
Example
The following program shows how to print the ASCII values using value property.
import Foundation import Glibc let val1 = UnicodeScalar("a").value print("ASCII value of a is ", val1) let val2 = UnicodeScalar("X").value print("ASCII value of X is ", val2)
Output
ASCII value of a is 97 ASCII value of X is 88
Here in the above code, we are finding the ASCII value of a and X using UnicodeScalar().value property that is 97 and 88.
- Related Articles
- Java Program to Print the ASCII values
- Kotlin Program to Print the ASCII values
- Haskell program to print ascii values
- C Program to print all ASCII values.
- C program to print the ASCII values in a string.
- How to print the ASCII values in Golang?
- Swift Program to Print the keys and values of a Dictionary
- Swift Program to Print an Array
- Swift program to print pascal’s triangle
- Swift program to print Hello World!
- Swift program to print spiral pattern
- Swift Program to Print a Set
- Swift Program to Print a Dictionary
- Swift Program to print the left diagonal matrix
- Swift Program to print the right diagonal matrix
