- 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
How to print the ASCII values in Golang?
In this tutorial, we are going to learn how to find and print the ASCII value of any character or symbol in Golang. The ASCII stands for American Standard Code for Information Exchange which is a way to represent the characters, and symbols in numeric form.
Printing the ASCII value using specifier
Algorithm
STEP 1 − Declaring the variable of string type
STEP 2 − Initializing the variable.
STEP 3 − Running the for loop which is printing the ASCII value for each element in the string.
Example 1
In this example, we are going to print the ASCII value of the character using the %d specifier.
package main // fmt package provides the function to print anything import ( "fmt" ) func main() { // declaring the variable of string type using the var keyword var introduction string // initializing the introduction string introduction = "TutorialsPoint" fmt.Println("ASCII of ",introduction,"is") // printing the ASCII value of each character using %d specifier for i := 0; i < len(introduction); i++ { fmt.Printf("The ASCII value of %c is %d \n", introduction[i], introduction[i]) } fmt.Println("(Printing the ASCII value using specifier)") }
Output
ASCII of TutorialsPoint is The ASCII value of T is 84 The ASCII value of u is 117 The ASCII value of t is 116 The ASCII value of o is 111 The ASCII value of r is 114 The ASCII value of i is 105 The ASCII value of a is 97 The ASCII value of l is 108 The ASCII value of s is 115 The ASCII value of P is 80 The ASCII value of o is 111 The ASCII value of i is 105 The ASCII value of n is 110 The ASCII value of t is 116 (Printing the ASCII value using specifier)
Description of code:
var introduction string − In this line, we are declaring the variable introduction of string type in which we will store the input from the user.
for i := 0; i < len(introduction); i++ {} − Running a for loop over the complete string from 0 till length of the string.
fmt.Printf("The ASCII value of %c is %d \n", introduction[i], introduction[i]) − Printing the ASCII value of each element in the string using the %d specifier
Printing the ASCII value using Tyoe Casting
Algorithm
STEP 1 − Declaring the variable of string type.
STEP 2 − Taking the input from the user by creating an object for the reader
STEP 3 − Running the for loop which is printing the ASCII value for each element in the string.
Example 2
In this example, we are going to print the ASCII value of the character using type casting.
package main // fmt package provides the function to print anything import ( "bufio" "fmt" "os" "strings" ) func main() { // declaring the variable of string type using the var keyword var dateOfBirth string fmt.Println("Can you please write down your Date of Birth?") // scanning the input by the user inputReader := bufio.NewReader(os.Stdin) dateOfBirth, _ = inputReader.ReadString('\n') // remove the delimiter from the string dateOfBirth = strings.TrimSuffix(dateOfBirth, "\n") // printing the ASCII value of each character using %d specifier for i := 0; i < len(dateOfBirth); i++ { fmt.Println("The ASCII value of", string(dateOfBirth[i]), "is", int(dateOfBirth[i])) } fmt.Println("(Printing the ASCII value using Type Casting)") }
Output
Can you please write down your Date of Birth? 27/05/1993 The ASCII value of 2 is 50 The ASCII value of 7 is 55 The ASCII value of / is 47 The ASCII value of 0 is 48 The ASCII value of 5 is 53 The ASCII value of / is 47 The ASCII value of 1 is 49 The ASCII value of 9 is 57 The ASCII value of 9 is 57The ASCII value of 3 is 51 (Printing the ASCII value using Type Casting)
Description of code:
var introduction string − In this line, we are declaring the variable introduction of string type in which we will store the input from the user.
inputReader := bufio.NewReader(os.Stdin)
introduction, _ = inputReader.ReadString('\n') − Creating a reader object and taking input from the user.
for i := 0; i < len(introduction); i++ {} − Running a for loop over the complete string from 0 till length of the string.
fmt.Println("The ASCII value of", string(dateOfBirth[i]), "is", int(dateOfBirth[i])) − Printing the ASCII value of each element in the string using the typecasting concept like this int(dateOfBirth[i]).
Conclusion
These are the two ways to print the ASCII values in Golang. To learn more about Go you can explore these tutorials.