- Char
- Functions
- compareTo
- dec
- equals
- hashCode
- inc
- minus
- plus
- rangeTo
- rangeUntil
- toByte
- toChar
- toDouble
- toFloat
- toInt
- toLong
- toShort
- toString
- Companion Object Properties
- MAX_CODE_POINT
- MAX_HIGH_SURROGATE
- MAX_LOW_SURROGATE
- MAX_RADIX
- MAX_SURROGATE
- MAX_VALUE
- MIN_CODE_POINT
- MIN_HIGH_SURROGATE
- MIN_LOW_SURROGATE
- MIN_RADIX
- MIN_SUPPLEMENTARY_CODE_POINT
- MIN_SURROGATE
- MIN_VALUE
- SIZE_BITS
- SIZE_BYTES
- Extension Properties
- code
- directionality
- Extension Functions
- coerceAtLeast
- coerceAtMost
- coerceIn
- compareTo
- digitToInt
- digitToIntOrNull
- Conversion between Char and Int in Kotlin
- 1. Char to Int
- 2. Int to Char
- Kotlin program to convert one character to integer
Char
On the JVM, non-nullable values of this type are represented as values of the primitive type char .
For Native
Represents a 16-bit Unicode character.
Functions
compareTo
Compares this value with the specified value for order.
dec
Returns this value decremented by one.
equals
Indicates whether some other object is «equal to» this one. Implementations must fulfil the following requirements:
hashCode
Returns a hash code value for the object. The general contract of hashCode is:
inc
Returns this value incremented by one.
minus
Subtracts the other Char value from this value resulting an Int.
Subtracts the other Int value from this value resulting a Char.
plus
Adds the other Int value to this value resulting a Char.
rangeTo
Creates a range from this value to the specified other value.
rangeUntil
Creates a range from this value up to but excluding the specified other value.
toByte
Returns the value of this character as a Byte .
toChar
Returns the value of this character as a Char .
toDouble
Returns the value of this character as a Double .
toFloat
Returns the value of this character as a Float .
toInt
Returns the value of this character as a Int .
toLong
Returns the value of this character as a Long .
toShort
Returns the value of this character as a Short .
toString
Returns a string representation of the object.
Companion Object Properties
MAX_CODE_POINT
The maximum value of a Unicode code point.
MAX_HIGH_SURROGATE
The maximum value of a Unicode high-surrogate code unit.
MAX_LOW_SURROGATE
The maximum value of a Unicode low-surrogate code unit.
MAX_RADIX
The maximum radix available for conversion to and from strings.
MAX_SURROGATE
The maximum value of a Unicode surrogate code unit.
MAX_VALUE
The maximum value of a character code unit.
MIN_CODE_POINT
The minimum value of a Unicode code point.
MIN_HIGH_SURROGATE
The minimum value of a Unicode high-surrogate code unit.
MIN_LOW_SURROGATE
The minimum value of a Unicode low-surrogate code unit.
MIN_RADIX
The minimum radix available for conversion to and from strings.
MIN_SUPPLEMENTARY_CODE_POINT
The minimum value of a supplementary code point, \u0x10000 .
MIN_SURROGATE
The minimum value of a Unicode surrogate code unit.
MIN_VALUE
The minimum value of a character code unit.
SIZE_BITS
The number of bits used to represent a Char in a binary form.
SIZE_BYTES
The number of bytes used to represent a Char in a binary form.
Extension Properties
code
Returns the code of this Char.
directionality
Returns the Unicode directionality property for the given character.
Extension Functions
coerceAtLeast
Ensures that this value is not less than the specified minimumValue.
coerceAtMost
Ensures that this value is not greater than the specified maximumValue.
coerceIn
Ensures that this value lies in the specified range minimumValue..maximumValue.
Ensures that this value lies in the specified range.
compareTo
Compares this object with the specified object for order. Returns zero if this object is equal to the specified other object, a negative number if it’s less than other, or a positive number if it’s greater than other.
digitToInt
Returns the numeric value of the decimal digit that this Char represents. Throws an exception if this Char is not a valid decimal digit.
Returns the numeric value of the digit that this Char represents in the specified radix. Throws an exception if the radix is not in the range 2..36 or if this Char is not a valid digit in the specified radix.
digitToIntOrNull
Returns the numeric value of the decimal digit that this Char represents, or null if this Char is not a valid decimal digit.
Returns the numeric value of the digit that this Char represents in the specified radix, or null if this Char is not a valid digit in the specified radix. Throws an exception if the radix is not in the range 2..36 .
Conversion between Char and Int in Kotlin
This article explores different ways to convert between char and int in Kotlin.
1. Char to Int
A simple solution to get the value of a character as an Int is with the toInt() function.
To convert a complete string to a sequence of bytes, use the getBytes() function, which returns a byte array.
To get the numeric value represented by the character in the specified radix, use the Character.digit() function.
2. Int to Char
To convert an Int value to a Char , use the toChar() function. The Int value should be in the range of Char codes Char.MIN_VALUE..Char.MAX_VALUE .
To get the corresponding character representation of the specified “digit” in the specified radix, use the Character.forDigit() function.
To convert the specified character (Unicode code point) to its UTF-16 representation, use the Character.toChars() function, which returns a char array.
That’s all about converting between char and int in Kotlin.
Average rating 5 /5. Vote count: 11
No votes so far! Be the first to rate this post.
We are sorry that this post was not useful for you!
Tell us how we can improve this post?
Thanks for reading.
Please use our online compiler to post code in comments using C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and help us grow. Happy coding 🙂
This website uses cookies. By using this site, you agree to the use of cookies, our policies, copyright terms and other conditions. Read our Privacy Policy. Got it
Kotlin program to convert one character to integer
In this tutorial, I will show you how to convert one character to integer in Kotlin. Kotlin provides a lot of utility functions for each class.
Method 1: Using Character.getNumericValue(char ch) :
This method takes one character as a parameter and returns its integer value.
fun main() val givenNumbers = charArrayOf('0', '1', '8', '7', '9') givenNumbers.forEach println("$it => $Character.getNumericValue(it)>") > >
It will print the below output :
We are converting each character of the character array givenNumbers.
Method 2: By converting it to a string :
Character class provides one method toInt, but it returns the ASCII value. But if we convert the character to a string and use the toInt method defined in the string class, it will convert that string to an integer.
Let’s check the below example :
fun main() val givenNumbers = charArrayOf('0', '1', '8', '7', '9') givenNumbers.forEach println("$it => $it.toString().toInt()>") > >
It will print the same output as the above example.