Add conversions

This commit is contained in:
Him188 2019-11-17 15:07:20 +08:00
parent 87ad509f70
commit d4bb6f1581

View File

@ -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
*/