From d4bb6f15815e0c9d6f2af373c4d46c2fd610e7ef Mon Sep 17 00:00:00 2001 From: Him188 <Him188@mamoe.net> Date: Sun, 17 Nov 2019 15:07:20 +0800 Subject: [PATCH] Add conversions --- .../utils/io/TypeConvertion.kt | 23 ++++++++++++------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/io/TypeConvertion.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/io/TypeConvertion.kt index ff1827c74..9bb5c02d4 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/io/TypeConvertion.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/io/TypeConvertion.kt @@ -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 */