Commit d4bb6f15 authored by Him188's avatar Him188

Add conversions

parent 87ad509f
......@@ -24,9 +24,6 @@ fun Int.toByteArray(): ByteArray = byteArrayOf(
(shr(0) and 0xFF).toByte()
)
/**
* 转 [ByteArray] 后再转 hex
*/
fun Int.toUHexString(separator: String = " "): String = this.toByteArray().toUHexString(separator)
/**
......@@ -39,14 +36,24 @@ fun UShort.toByteArray(): ByteArray = with(toUInt()) {
)
}
/**
* 转 [ByteArray] 后再转 hex.
*
* @return 2 位 Unsigned Hex
*/
fun Short.toUHexString(separator: String = " "): String = this.toUShort().toUHexString(separator)
fun UShort.toUHexString(separator: String = " "): String =
(this.toInt().shr(8).toUShort() and 255u).toByte().toUHexString() + separator + (this and 255u).toByte().toUHexString()
fun ULong.toUHexString(separator: String = " "): String =
this.toLong().toUHexString(separator)
fun Long.toUHexString(separator: String = " "): String =
this.ushr(32).toUInt().toUHexString(separator) + separator + this.ushr(32).toUInt().toUHexString(separator)
/**
* 255 -> 00 FF
*/
fun UByte.toByteArray(): ByteArray = byteArrayOf((this and 255u).toByte())
fun UByte.toUHexString(): String = (this and 255u).toByte().toUHexString()
/**
* 255u -> 00 00 00 FF
*/
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment