请问 Java 中的 hexDigits[(bt & 0xf0) >> 4]转换成 kotlin 要如何写呢?

2021-12-10 15:59:37 +08:00
 heavyrainn

请问 java 中的 hexDigits[(bt & 0xf0) >> 4]转换成 kotlin 要如何写呢? 其中 bt 是传入的一个 bytes[],hexDigits 是一个 char[],谢谢大家

1479 次点击
所在节点    程序员
12 条回复
Leviathann
2021-12-10 16:07:10 +08:00
java 里
bytes 是什么
数组怎么能 & 的
Leviathann
2021-12-10 16:09:13 +08:00
kotlin 里右移是中缀 shr
hizhengfu
2021-12-10 16:09:21 +08:00
idea 有自动转换的,可以 java 写下,然后贴到 kt 文件里面,搞几次就清楚怎么写了。
heavyrainn
2021-12-10 16:14:42 +08:00
@Leviathann 抱歉写错了,bt 是个 byte
heavyrainn
2021-12-10 16:15:27 +08:00
@hizhengfu 自动转换出来的结果不对呀…
heavyrainn
2021-12-10 16:16:38 +08:00
@hizhengfu idea 自动转化拿出来是 hexDigits[bt and 0xf0 shr 4],但是他自己都提示语法错误 o(╥﹏╥)o
Leviathann
2021-12-10 16:24:27 +08:00
16 进制后面 tobyte
and 完整体 toint 在右移
Leviathann
2021-12-10 16:25:15 +08:00
或者 bt 直接 toint 就行
mxalbert1996
2021-12-10 17:58:28 +08:00
hexDigits[bt.toInt().and(0xf0).ushr(4)]
注意 ushr 和 shr 的区别,这里很容易错
mxalbert1996
2021-12-10 18:01:05 +08:00
哦不过你这里应该没区别就是了
somebody
2021-12-11 01:42:32 +08:00
fun test(hexDigits: CharArray, bt: Byte) {
val index = (bt.toInt() and 0xF0) shr 4
hexDigits[index]
}
heavyrainn
2021-12-16 15:05:48 +08:00
好用了,感谢给给予帮助的人

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/821359

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX