ByteUtils

Utility class for byte operations commonly used in BLE protocols

Functions

Link copied to clipboard
fun bytesToHex(bytes: ByteArray, separator: String = " "): String

Convert bytes to hex string

Link copied to clipboard
fun calculateChecksum(data: ByteArray, offset: Int, length: Int): Byte

Calculate checksum for byte array

Link copied to clipboard
fun calculateXorChecksum(data: ByteArray, offset: Int, length: Int): Byte

Calculate XOR checksum for byte array

Link copied to clipboard
fun findPattern(data: ByteArray, pattern: ByteArray): Int

Find the index of a pattern in byte array

Link copied to clipboard
fun getSignedIntBE(data: ByteArray, offset: Int): Int

Get signed int (4 bytes) in big-endian format

Link copied to clipboard
fun getSignedIntLE(data: ByteArray, offset: Int): Int

Get signed int (4 bytes) in little-endian format

Link copied to clipboard
fun getSignedShortBE(data: ByteArray, offset: Int): Short

Get signed short (2 bytes) in big-endian format

Link copied to clipboard
fun getSignedShortLE(data: ByteArray, offset: Int): Short

Get signed short (2 bytes) in little-endian format

Link copied to clipboard
fun getUnsignedByte(data: ByteArray, offset: Int): Int

Get unsigned byte value (0-255)

Link copied to clipboard
fun getUnsignedIntBE(data: ByteArray, offset: Int): Long

Get unsigned int (4 bytes) in big-endian format

Link copied to clipboard
fun getUnsignedIntLE(data: ByteArray, offset: Int): Long

Get unsigned int (4 bytes) in little-endian format

Link copied to clipboard
fun getUnsignedShortBE(data: ByteArray, offset: Int): Int

Get unsigned short (2 bytes) in big-endian format

Link copied to clipboard
fun getUnsignedShortLE(data: ByteArray, offset: Int): Int

Get unsigned short (2 bytes) in little-endian format

Link copied to clipboard
fun hexToBytes(hexString: String): ByteArray

Convert hex string to byte array

Link copied to clipboard

Convert int to byte array in big-endian format

Link copied to clipboard

Convert int to byte array in little-endian format

Link copied to clipboard

Convert short to byte array in big-endian format

Link copied to clipboard

Convert short to byte array in little-endian format

Link copied to clipboard
fun startsWith(data: ByteArray, pattern: ByteArray): Boolean

Check if byte array starts with specific pattern

Link copied to clipboard
fun tryGetSignedByte(data: ByteArray, offset: Int): Int?
Link copied to clipboard
fun tryGetSignedIntBE(data: ByteArray, offset: Int): Int?
Link copied to clipboard
fun tryGetSignedIntLE(data: ByteArray, offset: Int): Int?
Link copied to clipboard
fun tryGetSignedShortBE(data: ByteArray, offset: Int): Short?
Link copied to clipboard
fun tryGetSignedShortLE(data: ByteArray, offset: Int): Short?
Link copied to clipboard
fun tryGetUnsignedByte(data: ByteArray, offset: Int): Int?
Link copied to clipboard
fun tryGetUnsignedIntBE(data: ByteArray, offset: Int): Long?
Link copied to clipboard
fun tryGetUnsignedIntLE(data: ByteArray, offset: Int): Long?
Link copied to clipboard
fun tryGetUnsignedShortBE(data: ByteArray, offset: Int): Int?
Link copied to clipboard
fun tryGetUnsignedShortLE(data: ByteArray, offset: Int): Int?