Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert char to ascii value in kotlin language

Tags:

android

kotlin

I am developing an android application with kotlin in which I need to convert an string character to its ASCII value,

fun tryDiCript(cypher: String) :String {
        var cypher = "fs2543i435u@$#g#@#sagb@!#12416@@@"
        var originalText = ""

        var regEx =Regex("[a-z]")
        for(char in  regEx.findAll(cypher))
        {                 
            originalText += (char.value.toInt()).toString()            
        }
       return originalText
}

this tutorial website showed me to use char.toInt() but it gives runtime error saying

Caused by: java.lang.NumberFormatException: Invalid int: "u"

so how if anyone knows hot to convert char to ASCII value please help me.

like image 890
Irony Stack Avatar asked Nov 11 '17 05:11

Irony Stack


People also ask

How do I convert to ASCII?

This is what makes it so easy to convert numbers to and from ASCII. If you have the ASCII code for a number you can either subtract 30h or mask off the upper four bits and you will be left with the number itself. Likewise you can generate the ASCII code from the number by adding 30h or by ORing with 30h.

What is ASCII value of A to Z?

The ASCII value of the lowercase alphabet is from 97 to 122. And, the ASCII value of the uppercase alphabet is from 65 to 90.

Why ASCII value of A is 97?

Each letter is assigned a number between 0 and 127. A upper and lower case character are assigned different numbers. For example the character A is assigned the decimal number 65, while a is assigned decimal 97 as shown below int the ASCII table.


1 Answers

char.value is a String. When you call String.toInt(), it is expecting a numeric string such as "1", "-123" to be parsed to Int. So, "f".toInt() will give you NumberFormatException since "f" isn't a numeric string.

If you are sure about char.value is a String containing exactly one character only. To get the ascii value of it, you can use:

char.value.first().code
like image 174
BakaWaii Avatar answered Oct 13 '22 22:10

BakaWaii