Scala Program to Find the ASCII Value of a Character

Posted in

Sangeeta Gulia
Last updated on January 13, 2023

    In this tutorial, you can learn how to write a Scala program to find and print the ASCII value of a character with the help of type casting.

    Before moving to the program, let us first understand what exactly ASCII and type casting are .

    What is ASCII?

    ASCII stands for American Standard Code for Information Interchange. It is a standard character encoding used in telecommunication. It is a seven-bit code that is used to represent alphanumeric data.

    As it is a seven-bit code, it represents 128 characters ( 2 ^ 7= 2 × 2 × 2 × 2 × 2 × 2 × 2 = 128 ). This character set mainly includes 26 uppercase letters (A to Z), 26 lowercase letters, 10 numerals (0 to 9), and 33 special characters, including mathematical symbols, punctuation marks, and the space character.

    Each letter, whether it is lowercase or uppercase, is assigned a value between 0 to 127. However, it is important to note that lowercase letters and uppercase letters have different values. For instance, ‘A’ has an ASCII value of 65, and ‘a’ has an ASCII value of 97.

    To convert a character to an ASCII value, the type feature comes in handy. Let us now understand what type casting is.

    What is Type Casting?

    Type casting is a method to convert one data type to another. Some people also refer to it with name type conversion.

    There are two categories of type casting, as follows:

    • Explicit Type Casting
    • Implicit Type Casting

    1. Implicit Type Casting

    Implicit type casting is when the compiler does the type conversion automatically based on the expression and the storage variable type. This conversion takes place from a smaller data type to a larger data type in the below sequence:

    2. Explicit Type Casting

    Explicit type conversion happens when we specify a type conversion method explicitly. For example: If one of the variables is ‘Int’, and we explicitly specify the toFloat() method on the integer type to convert it to float.

    Scala Program to Find the ASCII Value of a Character

    After understanding the type casting feature, you must be curious about how it can be helpful in ASCII conversion.

    So the answer is: ASCII values are the integer values. Hence, we can use type conversion from a character to an integer to get the ASCII values of the characters.

    As there are two categories of type casting, we can convert a character to an ASCII value using two methods: Explicit and Implicit type casting.

    Let us learn to write a Scala program for both of these methods of type conversion.

    1. Character to ASCII Conversion Using Explicit Type Casting

    objectASCIIConverterextendsApp {
    
    val characterInput: Char = 'a'
    val capitalCharacterInput: Char = 'A'
    
    println("Converting Character to ASCII using type casting ")
    println(characterInput.toInt)
    
    println("Converting Character to ASCII using type casting ")
    println('z'.toInt)
    
    println("Converting Character to ASCII using type casting ")
    println(capitalCharacterInput.toInt)
    
    println("Converting Character to ASCII using type casting ")
    println('Z'.toInt)
    
    }

    Output:

    ConvertingCharacter to ASCII using typecasting
    97
    ConvertingCharacter to ASCII using typecasting
    122
    ConvertingCharacter to ASCII using typecasting
    65
    ConvertingCharacter to ASCII using typecasting
    90

    In this above program, we used explicit type casting to get the ASCII values of the character with the help of the ‘toInt’ method.

    2. Character to ASCII Conversion Using Implicit Type Casting

    objectASCIIConverterextendsApp {
    
    val implicitlyCastedASCII: Int = '@'
    val asciiValueForPercentage: Int = '%'
    
    println("Converting Character to ASCII using implicit type casting ")
    println(implicitlyCastedASCII)
    
    println("Converting Character to ASCII using implicit type casting ")
    println(asciiValueForPercentage)
    
    }

    Output:

    ConvertingCharacter to ASCII using implicittypecasting
    64
    ConvertingCharacter to ASCII using implicittypecasting
    37

    In the above example, we converted a character to ASCII with the help of implicit conversion. The symbols ‘@’ and ‘%’ are of type Char. I have stored those symbols, @ and %, in variables named implicitlyCastedASCII and asciiValueForPercentage, respectively.

    Also, I have mentioned explicitly that these symbols should be of the type ‘Int’. So, a compiler needs to do the implicit type conversion in the assignment statements, which will lead to its ASCII values in the variables.

    Conclusion

    This was all about finding the ASCII value of a character in Scala. By referring to the above examples, you can try out similar sample programs to identify the ASCII values of other symbols.

    So, what are you waiting for? Try hands-on and explore more about characters and their ASCII values.

    Happy Coding!

    FAQs


    The ASCII value of the alphabet (small case) ranges from 97 to 122, where ‘a’ has the value 97 and ‘z’ takes 122.

    The ASCII value of the alphabet (Capital case) ranges from 65 to 90, where ‘A’ has the value 65 and ‘Z’ takes 90.

    Leave a Comment on this Post

    0 Comments