android - 在 Kotlin 中将 Int、Short、UInt、...等转换为字节或字节数组
问题描述
我在我的带有 Kotlin 的 Android 项目中使用 java nio ByteBuffer,我需要将所有原始类型转换为字节,以便我可以将它们放入 ByteBuffer,特别是 Unsigned 类型,因为 java nio 不支持 Unsigned 类型,如 UInt、UShort、 ...ETC。我知道以前应该问过这类问题,但我找不到。
解决方案
在 Kotlin 中,您可以尝试以下扩展功能:
println(12.toUInt())
println((-12).toUInt())
println(12.toUInt().toByte())
println((-12).toUInt().toByte())
输出:
I/System.out: 12
I/System.out: 4294967284
I/System.out: 12
I/System.out: -12
PS.:我在这里只使用了整数,但也有其他数据类型的扩展函数。
推荐阅读
- kubernetes - rules_k8s - k8s_deploy 模板远离图像
- python - Flask-SQLAlchemy 会话对象看不到数据库的更改?
- amazon-web-services - 如何将 API 网关与 Cognito 联合身份和 Cognito 用户池集成
- django - 是否可以仅重新启动 Django 中的特定应用程序,而不重新启动整个服务器?
- javascript - 如何在 JS 或 JQ 的地图中访问标记弹出文本?
- c# - Func 的示例和用法
,T> - security - 票务实施中的潜在安全漏洞
- html - 从网页单击超链接时如何运行python脚本?
- javascript - Blast.js 并创建 scrollintoview 和 opacity
- c# - 从 SQL 数据库创建 CSV 报告