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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With