Help us understand the problem. What is going on with this article?

KotlinでByteArrayをLong, Intに変換する

More than 1 year has passed since last update.

JVM環境の場合

JVM環境ではByteBufferクラスが使えるので、簡単にByteArrayを数値に変換することができます

jvm.kt
// ByteArray -> Long
val longBytes = byteArrayOf(1, 1, 1, 1, 1, 1, 1, 1)
ByteBuffer.wrap(longBytes).getLong()

// ByteArray -> Int
val intBytes = byteArrayOf(1, 1, 1, 1)
ByteBuffer.wrap(intBytes).getInt()

Kotlin/Native環境の場合

Kotlin/Native環境の場合はByteBufferクラスが使えないので、以下のような関数を実装します。

native.kt
// 8バイトの配列をLong型に変換します。
fun byteArrayToLong(byteArray: ByteArray): Long {
    var result: Long = 0
    for (i in 0..7) {
        result = result shl 8
        result = result or (byteArray[i] and 0xFF.toByte()).toLong()
    }
    return result
}

// 4バイトの配列をInt型に変換します。
fun byteArrayToInt(byteArray: ByteArray): Int {
    var result: Int = 0
    for (i in 0..3) {
        result = result shl 8
        result = result or (byteArray[i] and 0xFF.toByte()).toInt()
    }
    return result
}

テスト

test.kt
import kotlin.experimental.and
import java.nio.ByteBuffer

// 8バイトの配列をLong型に変換します。
fun byteArrayToLong(byteArray: ByteArray): Long {
    var result: Long = 0
    for (i in 0..7) {
        result = result shl 8
        result = result or (byteArray[i] and 0xFF.toByte()).toLong()
    }
    return result
}

// 4バイトの配列をInt型に変換します。
fun byteArrayToInt(byteArray: ByteArray): Int {
    var result: Int = 0
    for (i in 0..3) {
        result = result shl 8
        result = result or (byteArray[i] and 0xFF.toByte()).toInt()
    }
    return result
}

fun main(args: Array<String>){  
    val longBytes = byteArrayOf(1, 1, 1, 1, 1, 1, 1, 1)
    val intBytes = byteArrayOf(1, 1, 1, 1)

    println("ByteBuffer#getLong(): ${ByteBuffer.wrap(longBytes).getLong()}")
    println("ByteBuffer#getInt(): ${ByteBuffer.wrap(intBytes).getInt()}")

    println("byteArrayToLong(): ${byteArrayToLong(longBytes)}")
    println("byteArrayToInt(): ${byteArrayToInt(intBytes)}")
}

実行結果

[Running] cd "/Users/hoge" && kotlinc-jvm test.kt -include-runtime -d test.jar && java -jar test.jar
Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF-8
Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF-8
ByteBuffer#getLong(): 72340172838076673
ByteBuffer#getInt(): 16843009
byteArrayToLong(): 72340172838076673
byteArrayToInt(): 16843009

参考

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away