ibroadsword
ibroadsword

Reputation: 443

How do I convert a Char to Int?

So I have a String of integers that looks like "82389235", but I wanted to iterate through it to add each number individually to a MutableList. However, when I go about it the way I think it would be handled:

var text = "82389235"

for (num in text) numbers.add(num.toInt())

This adds numbers completely unrelated to the string to the list. Yet, if I use println to output it to the console it iterates through the string perfectly fine.

How do I properly convert a Char to an Int?

Upvotes: 44

Views: 35567

Answers (7)

LearningCharlito
LearningCharlito

Reputation: 378

toInt() is deprecated, a new simple way to do it is: 'digitToInt()'

var cad = "1234"
var sum = 0
for (char in cad){
      sum+= char.digitToInt() // output 10
      //sum+= chaar.toInt()   // output = 202 
 }

Upvotes: 0

axolotlKing0722
axolotlKing0722

Reputation: 289

toInt is deprecated now, use .code instead:

numbers.add(num.code - '0'.code)

Upvotes: 0

Eric-Wubbo Lameijer
Eric-Wubbo Lameijer

Reputation: 41

For clarity, the zeroAscii answer can be simplified to

val numbers = txt.map { it - '0' }

as Char - Char -> Int. If you are looking to minimize the number of characters typed, that is the shortest answer I know. The

val numbers = txt.map(Character::getNumericValue)

may be the clearest answer, though, as it does not require the reader to know anything about the low-level details of ASCII codes. The toString().toInt() option requires the least knowledge of ASCII or Kotlin but is a bit weird and may be most puzzling to the readers of your code (though it was the thing I used to solve a bug before investigating if there really wasn't a better way!)

Edit: I think Andrejs' answer (https://stackoverflow.com/a/70315079/14178883) is the best at the moment, I would only elaborate to give the full translation for the problem of the OP:

val text = "82389235"
val numbers = text.map { it.digitToInt() }

(I admit the OP wanted to add the numbers to a mutable list, in which case the last line would start with "numbers +=" instead of "val numbers =". But since it could be that the OP mainly cared about getting the numbers into a list and had simply chosen a mutable list since a for-loop required it, I give the example in what I think is generally best practice: making variables as immutable as possible).

Upvotes: 1

Andrejs
Andrejs

Reputation: 27727

Since Kotlin 1.5, there's a built-in function Char.digitToInt(): Int:

println('5'.digitToInt()) // 5 (int)

https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/digit-to-int.html

Upvotes: 24

s1m0nw1
s1m0nw1

Reputation: 82047

That's because num is a Char, i.e. the resulting values are the ascii value of that char.

This will do the trick:

val txt = "82389235"
val numbers = txt.map { it.toString().toInt() }

The map could be further simplified:

map(Character::getNumericValue)

Upvotes: 54

Vadzim
Vadzim

Reputation: 26200

On JVM there is efficient java.lang.Character.getNumericValue() available:

val numbers: List<Int> = "82389235".map(Character::getNumericValue)

Upvotes: 9

zsmb13
zsmb13

Reputation: 89628

The variable num is of type Char. Calling toInt() on this returns its ASCII code, and that's what you're appending to the list.

If you want to append the numerical value, you can just subtract the ASCII code of 0 from each digit:

numbers.add(num.toInt() - '0'.toInt())

Which is a bit nicer like this:

val zeroAscii = '0'.toInt()
for(num in text) {
    numbers.add(num.toInt() - zeroAscii)
}

This works with a map operation too, so that you don't have to create a MutableList at all:

val zeroAscii = '0'.toInt()
val numbers = text.map { it.toInt() - zeroAscii }

Alternatively, you could convert each character individually to a String, since String.toInt() actually parses the number - this seems a bit wasteful in terms of the objects created though:

numbers.add(num.toString().toInt())

Upvotes: 10

Related Questions