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.

<div class="execute"></div><div class="code-mirror  language-java" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token keyword">package</span> <span class="token namespace">main</span>

<span class="token comment">// fmt package provides the function to print anything</span>
<span class="token keyword">import</span> <span class="token punctuation">(</span>
   <span class="token string">"fmt"</span>
<span class="token punctuation">)</span>
func <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

   <span class="token comment">// declaring the variable of string type using the var keyword</span>
   <span class="token keyword">var</span> introduction string

   <span class="token comment">// initializing the introduction string</span>
   introduction <span class="token operator">=</span> <span class="token string">"TutorialsPoint"</span>
   <span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"ASCII of "</span><span class="token punctuation">,</span>introduction<span class="token punctuation">,</span><span class="token string">"is"</span><span class="token punctuation">)</span>

   <span class="token comment">// printing the ASCII value of each character using %d specifier</span>
   <span class="token keyword">for</span> i <span class="token operator">:</span><span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator"><</span> <span class="token function">len</span><span class="token punctuation">(</span>introduction<span class="token punctuation">)</span><span class="token punctuation">;</span> i<span class="token operator">++</span> <span class="token punctuation">{</span>
      <span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Printf</span><span class="token punctuation">(</span><span class="token string">"The ASCII value of %c is %d \n"</span><span class="token punctuation">,</span> introduction<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">,</span> introduction<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span>
   <span class="token punctuation">}</span>
   <span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"(Printing the ASCII value using specifier)"</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>

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.

<div class="code-mirror  language-java" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token keyword">package</span> <span class="token namespace">main</span>

<span class="token comment">// fmt package provides the function to print anything</span>
<span class="token keyword">import</span> <span class="token punctuation">(</span>
   <span class="token string">"bufio"</span>
   <span class="token string">"fmt"</span>
   <span class="token string">"os"</span>
   <span class="token string">"strings"</span>
<span class="token punctuation">)</span>
func <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

   <span class="token comment">// declaring the variable of string type using the var keyword</span>
   <span class="token keyword">var</span> dateOfBirth string
   <span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"Can you please write down your Date of Birth?"</span><span class="token punctuation">)</span>
   
   <span class="token comment">// scanning the input by the user</span>
   inputReader <span class="token operator">:</span><span class="token operator">=</span> <span class="token class-name"><span class="token namespace">bufio<span class="token punctuation">.</span></span>NewReader</span><span class="token punctuation">(</span><span class="token class-name"><span class="token namespace">os<span class="token punctuation">.</span></span>Stdin</span><span class="token punctuation">)</span>
   dateOfBirth<span class="token punctuation">,</span> _ <span class="token operator">=</span> <span class="token class-name"><span class="token namespace">inputReader<span class="token punctuation">.</span></span>ReadString</span><span class="token punctuation">(</span><span class="token string">'\n'</span><span class="token punctuation">)</span>
   
   <span class="token comment">// remove the delimiter from the string</span>
   dateOfBirth <span class="token operator">=</span> <span class="token class-name"><span class="token namespace">strings<span class="token punctuation">.</span></span>TrimSuffix</span><span class="token punctuation">(</span>dateOfBirth<span class="token punctuation">,</span> <span class="token string">"\n"</span><span class="token punctuation">)</span>
   
   <span class="token comment">// printing the ASCII value of each character using %d specifier</span>
   <span class="token keyword">for</span> i <span class="token operator">:</span><span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator"><</span> <span class="token function">len</span><span class="token punctuation">(</span>dateOfBirth<span class="token punctuation">)</span><span class="token punctuation">;</span> i<span class="token operator">++</span> <span class="token punctuation">{</span>
      <span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"The ASCII value of"</span><span class="token punctuation">,</span> <span class="token function">string</span><span class="token punctuation">(</span>dateOfBirth<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">"is"</span><span class="token punctuation">,</span> <span class="token keyword">int</span><span class="token punctuation">(</span>dateOfBirth<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
   <span class="token punctuation">}</span>
   <span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"(Printing the ASCII value using Type Casting)"</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</div>

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.

Updated on: 2022-08-29T08:08:07+05:30

9K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements