GO Program to find the ASCII value of a character

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

How to find the ASCII value of a character

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.
Computer stores ASCII value of characters (number between 0 and 127) rather than the character itself. The ASCII value of lower case alphabets are from 97 to 122 and the ASCII value of uppercase alphabets are from 65 to 90. When we compare variable ‘a’ to ‘z’ and ‘A’ to ‘Z’, the variable is compared with the ASCII value of the alphabets 97 to 122 and 65 to 90 respectively.

func (r *Reader) ReadByte() (byte, error) 

Here ReadByte implements the io.ByteReader interface.

 

How to find the ASCII value of a character in the Go Program

We are using fmt.println() function for printing the string to the output screen. Here we are showing how to find the ASCII value of a character in the Go language. Here variable ch is used to hold the read character. For reading the character create the reader using NewReader. Read the character as ch, _:= reader.ReadByte().

func (r *Reader) ReadByte() (byte, error) 

Here ReadByte implements the io.ByteReader interface.

Finally print the results. Given below are the steps which are used in the Go program. 

ALGORITHM

STEP 1: Import the package fmt, bufio, os

STEP 3: Create a reader using NewReader as reader := bufio.NewReader(os.Stdin)

STEP 4: Read the character into variable ch as ch, _ := reader.ReadByte()

STEP 5: Print the ASCII value of the character using fmt.printf()

 

GO Source Code

                                          package main

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

func main() {

    reader := bufio.NewReader(os.Stdin)

    fmt.Print("Enter any character = ")
    ch, _ := reader.ReadByte()

    fmt.Printf("The ASCII value of %c = %d\n", ch, ch)
}
                                      

OUTPUT

Enter any character = j

The ASCII value of j = 106