Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
M
Mirai
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages
Packages
List
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issues
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
MyCard
Mirai
Commits
fb5b5241
Commit
fb5b5241
authored
Feb 29, 2020
by
Him188
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Simplify convention
parent
1e9b34a6
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
20 additions
and
9 deletions
+20
-9
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/io/conversion.kt
.../commonMain/kotlin/net.mamoe.mirai/utils/io/conversion.kt
+20
-9
No files found.
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/io/conversion.kt
View file @
fb5b5241
...
@@ -35,10 +35,10 @@ fun Short.toByteArray(): ByteArray = with(toInt()) {
...
@@ -35,10 +35,10 @@ fun Short.toByteArray(): ByteArray = with(toInt()) {
* 255 -> 00 00 00 FF
* 255 -> 00 00 00 FF
*/
*/
fun
Int
.
toByteArray
():
ByteArray
=
byteArrayOf
(
fun
Int
.
toByteArray
():
ByteArray
=
byteArrayOf
(
(
shr
(
24
)
and
0
xFF
).
toByte
(),
ushr
(
24
).
toByte
(),
(
shr
(
16
)
and
0
xFF
).
toByte
(),
ushr
(
16
).
toByte
(),
(
shr
(
8
)
and
0
xFF
).
toByte
(),
ushr
(
8
).
toByte
(),
(
shr
(
0
)
and
0
xFF
).
toByte
()
ushr
(
0
).
toByte
()
)
)
/**
/**
...
@@ -114,7 +114,8 @@ fun Byte.fixToUHex(): String = this.toUByte().fixToUHex()
...
@@ -114,7 +114,8 @@ fun Byte.fixToUHex(): String = this.toUByte().fixToUHex()
/**
/**
* 转无符号十六进制表示, 并补充首位 `0`.
* 转无符号十六进制表示, 并补充首位 `0`.
*/
*/
fun
UByte
.
fixToUHex
():
String
=
if
(
this
.
toInt
()
in
0
..
15
)
"0${this.toString(16).toUpperCase()}"
else
this
.
toString
(
16
).
toUpperCase
()
fun
UByte
.
fixToUHex
():
String
=
if
(
this
.
toInt
()
in
0
..
15
)
"0${this.toString(16).toUpperCase()}"
else
this
.
toString
(
16
).
toUpperCase
()
/**
/**
* 将无符号 Hex 转为 [ByteArray], 有根据 hex 的 [hashCode] 建立的缓存.
* 将无符号 Hex 转为 [ByteArray], 有根据 hex 的 [hashCode] 建立的缓存.
...
@@ -143,7 +144,9 @@ fun String.chunkedHexToBytes(): ByteArray =
...
@@ -143,7 +144,9 @@ fun String.chunkedHexToBytes(): ByteArray =
* 这个方法很累, 不建议经常使用.
* 这个方法很累, 不建议经常使用.
*/
*/
fun
String
.
autoHexToBytes
():
ByteArray
=
fun
String
.
autoHexToBytes
():
ByteArray
=
this
.
replace
(
"\n"
,
""
).
replace
(
" "
,
""
).
asSequence
().
chunked
(
2
).
map
{
(
it
[
0
].
toString
()
+
it
[
1
]).
toUByte
(
16
).
toByte
()
}.
toList
().
toByteArray
()
this
.
replace
(
"\n"
,
""
).
replace
(
" "
,
""
).
asSequence
().
chunked
(
2
).
map
{
(
it
[
0
].
toString
()
+
it
[
1
]).
toUByte
(
16
).
toByte
()
}.
toList
().
toByteArray
()
/**
/**
* 将无符号 Hex 转为 [UByteArray], 有根据 hex 的 [hashCode] 建立的缓存.
* 将无符号 Hex 转为 [UByteArray], 有根据 hex 的 [hashCode] 建立的缓存.
...
@@ -188,16 +191,24 @@ fun getRandomString(length: Int, vararg charRanges: CharRange): String =
...
@@ -188,16 +191,24 @@ fun getRandomString(length: Int, vararg charRanges: CharRange): String =
* 本函数将 4 个 [Byte] 的 bits 连接得到 [Int]
* 本函数将 4 个 [Byte] 的 bits 连接得到 [Int]
*/
*/
fun
ByteArray
.
toUInt
():
UInt
=
fun
ByteArray
.
toUInt
():
UInt
=
(
this
[
0
].
toUInt
().
and
(
255
u
)
shl
24
)
+
(
this
[
1
].
toUInt
().
and
(
255
u
)
shl
16
)
+
(
this
[
2
].
toUInt
().
and
(
255
u
)
shl
8
)
+
(
this
[
3
].
toUInt
().
and
(
255
u
)
shl
0
)
(
this
[
0
].
toUInt
().
and
(
255
u
)
shl
24
)
+
(
this
[
1
].
toUInt
().
and
(
255
u
)
shl
16
)
+
(
this
[
2
].
toUInt
().
and
(
255
u
)
shl
8
)
+
(
this
[
3
].
toUInt
().
and
(
255
u
)
shl
0
)
fun
ByteArray
.
toUShort
():
UShort
=
fun
ByteArray
.
toUShort
():
UShort
=
((
this
[
0
].
toUInt
().
and
(
255
u
)
shl
8
)
+
(
this
[
1
].
toUInt
().
and
(
255
u
)
shl
0
)).
toUShort
()
((
this
[
0
].
toUInt
().
and
(
255
u
)
shl
8
)
+
(
this
[
1
].
toUInt
().
and
(
255
u
)
shl
0
)).
toUShort
()
fun
ByteArray
.
toInt
():
Int
=
fun
ByteArray
.
toInt
():
Int
=
(
this
[
0
].
toInt
().
and
(
255
)
shl
24
)
+
(
this
[
1
].
toInt
().
and
(
255
)
shl
16
)
+
(
this
[
2
].
toInt
().
and
(
255
)
shl
8
)
+
(
this
[
3
].
toInt
().
and
(
255
)
shl
0
)
(
this
[
0
].
toInt
().
and
(
255
)
shl
24
)
+
(
this
[
1
].
toInt
().
and
(
255
)
shl
16
)
+
(
this
[
2
].
toInt
().
and
(
255
)
shl
8
)
+
(
this
[
3
].
toInt
().
and
(
255
)
shl
0
)
/**
/**
* 从 [IoBuffer.Pool] [borrow][ObjectPool.borrow] 一个 [IoBuffer] 然后将 [this] 写入.
* 从 [IoBuffer.Pool] [borrow][ObjectPool.borrow] 一个 [IoBuffer] 然后将 [this] 写入.
* 注意回收 ([ObjectPool.recycle])
* 注意回收 ([ObjectPool.recycle])
*/
*/
fun
ByteArray
.
toIoBuffer
(
offset
:
Int
=
0
,
length
:
Int
=
this
.
size
-
offset
,
pool
:
ObjectPool
<
IoBuffer
>
=
IoBuffer
.
Pool
):
IoBuffer
=
pool
.
borrow
().
let
{
it
.
writeFully
(
this
,
offset
,
length
);
it
}
fun
ByteArray
.
toIoBuffer
(
\ No newline at end of file
offset
:
Int
=
0
,
length
:
Int
=
this
.
size
-
offset
,
pool
:
ObjectPool
<
IoBuffer
>
=
IoBuffer
.
Pool
):
IoBuffer
=
pool
.
borrow
().
let
{
it
.
writeFully
(
this
,
offset
,
length
);
it
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment