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"

所以,如果有人知道如何将字符转换成ASCII值,请帮助我.

推荐答案

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

Kotlin相关问答推荐

等待下一个值时暂停Kotlin Coroutine

Spring Boot kotlin协程不能并行运行

在 detekt 配置文件中找不到某些属性

Kotlin 启动与启动(Dispatchers.Default)

如何创建扩展函数isNullOrEmpty?

T except one class

Kotlin:不允许在辅助构造函数参数上使用val

listOf() 返回 MutableList

.indices 在 kotlin 中的含义是什么?

Kotlin:泛型、反射以及类型 T 和 T:Any 之间的区别

什么是 .kotlin_builtins 文件,我可以从我的 uberjars 中省略它们吗?

Kotlin 中的数据类

Kotlin DataBinding 将静态函数传递到布局 xml

@uncheckedVariance 在 Kotlin 中?

重复构建失败To use Coroutine features, you must add `ktx`......

Kotlin中的Memoization功能

指定为非null的参数在ArrayAdaper中为null

如何修复未解析的参考生命周期范围?

带有注释为@RegisterExtension的字段的 JUnit 5 测试在 Kotlin 中不起作用

RxJava - 每秒发出一个 observable