如何在 kotlin 中将 uLong(64 位)转换为字节数组

问题描述 投票:0回答:1

我需要写一些东西到 BLE 设备,只能接受字节数组。但是我想写一个unsigned long类型的整数,如何将它转换成字节数组呢?提前谢谢你。

kotlin long-integer
1个回答
0
投票

应该这样做:

var source: ULong = 0uL            
val bytes = ByteArray(5)
bytes[0x0] = (source shr 32).toByte()
bytes[0x1] = (source shr 24).toByte()
bytes[0x2] = (source shr 16).toByte()
bytes[0x3] = (source shr 8).toByte()
bytes[0x4] = source.toByte()

取决于您是否希望在索引 0 等处填充更高顺序的字节

© www.soinside.com 2019 - 2024. All rights reserved.