GO Program to print ASCII value of each string character

For a better understanding of this example, we always recommend you to learn the basic topics of Golang programming listed below:

What is ASCII value?

ASCII or American Standard Code For Information Interchange is a character encoding-scheme.Most modern character-encoding schemes are based on ASCII.ASCII was developed from telegraphic codes. ASCII encodes 128 specified characters into seven-bit integers.The characters encoded are numbers 0 to 9, lowercase letters a to z, uppercase letters A to Z. ASCII includes definitions for 128 characters: 33 are non-printing control characters.This character is mostly used in digital electronics.

In the case of every programming there is an integer value to represent the character,that integer value is known as an ASCII value of the character.For example,ASCII value of the character 'a' is 97.If a character is stored in variable of type char, the ASCII value of character is stored instead of that character itself.In the above case the character 'a' is stored in the char type variable, ASCII value of that character is stored which is 97.In, this program user is asked to enter a character and this program will display the ASCII value of that character.ASCII code is the combination of both source code and channel code.The 8 bit in an ASCII code is used for error detection.Parity check bit is generally used for error detection process.Parity bits are of two types.They are:

1. Even parity bit

2. Odd parity bit

ASCII code also provides encoding of 128 characters.Here the error involving single bit can be detected but cannot be corrected.To recover from an error, the receiver would request re transmission of incorrect code words.

 

How to print the ASCII value of each string character in the GO Program

Here we are showing how to print the ASCII value of each string character in the Go language. Here variable str is holding the string need to be converted. And the reader is an object created for reading. Use for loop for printing the ASCII value of each character in the string.

syntax for for loop


for    initializer; condition;   incrementor {
}

Given below are the steps which are used in the Go program. 

ALGORITHM

STEP 1: Import the package fmt, bufio, os

STEP 2: Start function main()

STEP 3: Create an object reader for input reading

STEP 4: Read the string into the variable str

STEP 5: Use for loop for printing string characters and its ASCII by using $c, %d

 

GO Source Code

                                          package main

import (
    "bufio"
    "fmt"
    "os"
)

func main() {

    reader := bufio.NewReader(os.Stdin)
    fmt.Print("Enter the string to find ASCII = ")
    str, _ := reader.ReadString('\n')
    for i := 0; i < len(str); i++ {
        fmt.Printf("The ASCII value of %c = %d\n", str[i], str[i])
    }
}
                                      

OUTPUT

Enter the string to find ASCII = hello
The ASCII value of h = 104
The ASCII value of e = 101
The ASCII value of l = 108
The ASCII value of l = 108
The ASCII value of o = 111