Go Program to Print ASCII value of String Characters

Write a Go Program to Print the ASCII value of total String Characters. The for loop (for i := 0; i < len(strData); i++) iterate string characters from start to string length. Within the loop, the printf statement prints the ASCII values. For this, we use the %d or %v string formatting option.

package main

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

func main() {

    reader := bufio.NewReader(os.Stdin)

    fmt.Print("Enter Any String to find ASCII Values = ")
    strData, _ := reader.ReadString('\n')

    for i := 0; i < len(strData); i++ {
        fmt.Printf("The ASCII Value of %c = %d\n", strData[i], strData[i])
    }
}
Enter Any String to find ASCII Values = 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

Go Program to Print ASCII value of String Characters using for loop range

package main

import "fmt"

func main() {

    var strData string

    strData = "Golang Programs"

    for _, c := range strData {
        fmt.Printf("The ASCII Value of %c = %d\n", c, c)
    }
}
Go Program to Print ASCII Value of String Characters

This Golang program to return the ASCII value of String Characters is the same as the first example. However, we used the %v instead of %d.

package main

import (
    "fmt"
)

func main() {

    strData := "Tutorial Gateway"

    for i := 0; i < len(strData); i++ {
        fmt.Printf("The ASCII Value of %c = %v\n", strData[i], strData[i])
    }
}
The ASCII Value of T = 84
The ASCII Value of u = 117
The ASCII Value of t = 116
The ASCII Value of o = 111
The ASCII Value of r = 114
The ASCII Value of i = 105
The ASCII Value of a = 97
The ASCII Value of l = 108
The ASCII Value of   = 32
The ASCII Value of G = 71
The ASCII Value of a = 97
The ASCII Value of t = 116
The ASCII Value of e = 101
The ASCII Value of w = 119
The ASCII Value of a = 97
The ASCII Value of y = 121