# Char.toByte() - Conversion of Char to Number is deprecated?

**URL:** <https://discuss.kotlinlang.org/t/char-tobyte-conversion-of-char-to-number-is-deprecated/23465>\
**Category:** Libraries\
**Created:** [November 16, 2021, 10:28am UTC](https://discuss.kotlinlang.org/t/char-tobyte-conversion-of-char-to-number-is-deprecated/23465 "2021-11-16T10:28:31Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![pasn1ak](https://avatars.discourse-cdn.com/v4/letter/p/ecd19e/32.png) [@pasn1ak](https://discuss.kotlinlang.org/u/pasn1ak)\
**Post date:** [November 16, 2021, 10:28am UTC](https://discuss.kotlinlang.org/t/char-tobyte-conversion-of-char-to-number-is-deprecated/23465/1 "2021-11-16T10:28:31Z")

</div>

`' '.toByte() returns Byte`

The doc (below) states “Use Char.code property instead.” but

`' '.code returns Int`

I need a ASCII byte for comparison in ByteArray (in some low level network code). So should I use `' '.code.toByte()`? If so doc/deprecated message could be improved…

> **[toByte - Kotlin Programming Language](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-char/to-byte.html)**

`@DeprecatedSinceKotlin("1.5") fun toByte(): Byte`  
[(Common source)](https://github.com/JetBrains/kotlin/tree/34e57a45f2d4283be572137b4b497414b8833ee7/core/builtins/native/kotlin/Char.kt#L61) [(Native source)](https://github.com/JetBrains/kotlin/tree/34e57a45f2d4283be572137b4b497414b8833ee7/kotlin-native/runtime/src/main/kotlin/kotlin/Char.kt#L60)

**Deprecated:** Conversion of Char to Number is deprecated. **Use Char.code property instead.**

Returns the value of this character as a `Byte` .

---

<div class="post-metadata">

**Author:** ![broot](https://avatars.discourse-cdn.com/v4/letter/b/a88e57/32.png) [@broot](https://discuss.kotlinlang.org/u/broot)\
**Post date:** [November 16, 2021, 12:19pm UTC](https://discuss.kotlinlang.org/t/char-tobyte-conversion-of-char-to-number-is-deprecated/23465/2 "2021-11-16T12:19:59Z")

</div>

Depreciation is very clear on what is the replacement:

```auto
@Deprecated("Conversion of Char to Number is deprecated. Use Char.code property instead.", ReplaceWith("this.code.toByte()"))

```

IntelliJ even offers to replace the code automatically with alt+enter / clicking on a bulb.

Also, note that a character generally can’t be stored in just a single byte. If you are sure you’re dealing with ASCII then I believe `code.toByte()` should be fine. Still, it is probably safer and more reliable to convert using ASCII explicitly:

```kotlin
"foo".toByteArray(Charset.forName("US-ASCII"))

```

In the case of chars that are not available in ASCII (think emojis 😉 ), it will replace then with `?` or ignore entirely. `code.toByte()` will generate random/gibberish chars.

---

<div class="post-metadata">

**Author:** ![skylen](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/skylen/32/6609_2.png) [@skylen](https://discuss.kotlinlang.org/u/skylen)\
**Post date:** [November 16, 2021, 11:23pm UTC](https://discuss.kotlinlang.org/t/char-tobyte-conversion-of-char-to-number-is-deprecated/23465/3 "2021-11-16T23:23:52Z")

</div>

Be careful to distinguish between a character and a byte. In Kotlin (and Java & JavaScript too) a character is 16 bits (it’s a UTF-16 “code unit”) which makes it easier to work with non-ASCII characters.

---

<div class="post-metadata">

**Author:** ![pasn1ak](https://avatars.discourse-cdn.com/v4/letter/p/ecd19e/32.png) [@pasn1ak](https://discuss.kotlinlang.org/u/pasn1ak)\
**Post date:** [November 17, 2021, 12:55am UTC](https://discuss.kotlinlang.org/t/char-tobyte-conversion-of-char-to-number-is-deprecated/23465/4 "2021-11-17T00:55:58Z")

</div>

> [@broot](#):
>
> `toByteArray`

Thanks! I am dealing with large binary blob java.nio.MappedByteBuffer which I am searching for a few signature bytes themselves characters. In C++ I would go for find() but MBB has no such goodies… no `search` nor `find(ByteArray)`…
