Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
438 views
in Technique[技术] by (71.8m points)

android - How to put a ULong to a ByteBuffer in Kotlin

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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*().


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...