Kotlin Program to Print the ASCII values


In this article, we will understand how to print ascii values of characters. This is done by assigning the character to an integer value and printing those integer values.

Below is a demonstration of the same

Suppose our input is −

Enter a character: s

The desired output would be −

Ascii value of s is 115

Algorithm

  • Step 1 − Start

  • Step 2 − Declare a char as input

  • Step 3 − Define the character

  • Step 4 − Assign the character to an integer variable using toInt() function and store it in myResult.

  • Step 5 − Display the result

  • Step 6 − Stop

Example 1

In this example, we will print the ASCII values using the toInt() method in Kotlin. First, set the variable for input character −

val input = 's'

Now, use the toInt() method to display the ASCII values −

val myResult = input.toInt()

Let us now see the complete example to print the ASCII values

fun main() { val input = 's' println("The input value is defined as: $input") val myResult = input.toInt() println("The ASCII value of $input is: $myResult") }

Output

The input value is defined as: s
The ASCII value of s is: 115

Example 2

In this example, we will print the ASCII values −

fun main() { val input = 's' println("The input value is defined as: $input") getASCII(input) } fun getASCII(input: Char) { val myResult = input.toInt() println("The ASCII value of $input is: $myResult") }

Output

The input value is defined as: s
The ASCII value of s is: 115

Updated on: 13-Oct-2022

563 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements