【发布时间】:2021-09-14 18:21:00
【问题描述】:
Kotlin 1.5 将 16 位整数转换为长度为 2 的字节的命令是什么?第二个问题是 outputstream 在末尾需要一个字符串,因此它可以使用 toByteArray() 进行转换
# Original Python Code
...
i = int((2**16-1)*ratio) # 16 bit int
i.to_bytes(2, byteorder='big')
output = (i).to_bytes(2, byteorder='big')
# Kotlin Code so far
var i = ((2.0.pow(16) - 1) * ratio).toInt() // Convert to 16 bit Integer
print("16 bit Int: " + i)
output = .....
....
...
val outputStream: OutputStream = socket.getOutputStream()
outputStream.write(output.toByteArray()) // write requires ByteArray for some reason
【问题讨论】:
标签: arrays sockets kotlin binary outputstream