ditchoom / buffer Goto Github PK
View Code? Open in Web Editor NEWKotlin Multiplatform bytebuffer/byte[] wrapper
License: Apache License 2.0
Kotlin Multiplatform bytebuffer/byte[] wrapper
License: Apache License 2.0
Hi,
I want to reuse a Java implementation which converts a ByteBuffer to a FloatBuffer. How can I achieve this?
Thanks.
Please add linux arm64 support
Here is my test code
import com.d10ng.common.base.toByteArrayFromHex
import com.d10ng.common.base.toHexString
import com.d10ng.common.base.toUnsignedInt
import com.d10ng.common.base.toUnsignedLong
import com.ditchoom.buffer.PlatformBuffer
import com.ditchoom.buffer.wrap
import kotlin.test.Test
class Test {
@Test
fun test1() {
val str = "0001A960DBD8A500006500006400010A003132333435363738"
val hex = str.toByteArrayFromHex()
val buf = PlatformBuffer.wrap(hex)
val messageId = buf.readByteArray(2).toUnsignedInt()
val cmd = buf.readByte()
println("messageId: $messageId, cmd: $cmd")
val data = buf.readByteArray(buf.remaining())
println("data=${data.toHexString()}")
val buf1 = PlatformBuffer.wrap(data)
println("buf1.readByteArray(4)=${buf1.readByteArray(4).toHexString()}")
//val timestamp = buf1.readByteArray(4).toUnsignedLong() * 1000L
//println("timestamp: $timestamp")
}
}
The output log is as follows:
messageId: 1, cmd: -87
data=60DBD8A500006500006400010A003132333435363738
buf1.readByteArray(4)=0001A960
But the expected buf1.readByteArray(4) output is 60DBD8A5
The ByteBuffer functions that allow fetching a data item from a specific position without moving the pointers are needed to make this a useful replacement for ByteBuffer, e.g. ByteBuffer.get(index)
returns the byte at index
without moving the position
.
I'm using this library to read bluetooth values, in some standards exist this type of value.
uint24. There are ways to read this, with this library.
Using version 1.3.9
Having following test
@Test
fun test() {
val text = "Hello world!"
val input = PlatformBuffer.wrap(text.encodeToByteArray())
val output = PlatformBuffer.allocate(text.length)
output.write(input)
assertThat(input.position()).isEqualTo(text.length) //<- ios fails
assertThat(output.position()).isEqualTo(text.length)
output.position(0)
assertThat(output.readString(text.length)).isEqualTo(text)
}
JVM: OK
iOS: fails
com.willowtreeapps.opentest4k.AssertionFailedError: expected:<[12]> but was:<[0]>
Expected :[12]
Actual :[0]
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.