How to put a ULong to a ByteBuffer in Kotlin

401 views Asked by At

I just the answer in this post How to convert a Data Class to ByteBuffer in Kotlin?

And it works as expected. The problem is that nearly all data types are possible to put except the unsigned once. There are putLong etc. functions for bytebuffers but not for putULong.

Any hint would be apreciated.

Kind regards,

C.W.

1

There are 1 answers

0
Alexey Romanov On

There is an one-to-one correspondence between ULong and Long, so you can convert ULong to Long, put that into the buffer, and vice versa when reading. For convenience, declare these extension functions:

fun ByteBuffer.putULong(value: ULong): ByteBuffer = putLong(value.toLong())

fun ByteBuffer.getULong(): ULong = getLong().toULong()

and they can be called just like get/put*().