kotlin将bytearray转换为String数据崩溃 [英] kotlin Converts bytearray to String data crash
问题描述
将通过通信接收的字节数组数据转换为String.
Converts bytearray data received through communication into String.
如果字节数组很长(例如355),则结尾将被截断.
If the byte array is long (ex 355), the end is truncated.
utf-8,utf-16等也是如此.
Same goes for utf-8, utf-16 etc.
fun getString(content:ByteArray, length: Int): String? {
var str: String? = null
try {
str = String(content, 0, length, Charset.forName("euc-kr"))
i("Test::getString str : " + str)
} catch (e: java.lang.Exception) {
i("Test::getString ERR ")
error("Charset exception", DlgAprList::class.java)
}
return str
}
发生在超过355个字节时,如果查看附件链接的图像,它为389个字节,则可以看到被截断了.
Occurs when it exceeds 355 bytes, if you look at the image of the attached link, it was 389 bytes and you can see that truncation occurred.
在其他函数中,请像这样使用getString函数.
In other functions, use the getString function like this.
var str : String? = getString(data, data.size)
var info: Array<String> = str!!.split(";").toTypedArray()
for (i in 0 until info.size - 1) {
var temp = info[i].split("-".toRegex()).toTypedArray()
var name = temp[0]
var desc = temp[1]
var final = name +"-"+ desc
var finalresult : DlgAprInfo = DlgAprInfo(name,desc)
i("Test::parseAndReplace temp size : " + info.size + " temp " + i +" : " + finalresult.name + "-"+ finalresult.desc)
//listnew.add(final)
list.add(finalresult) // testver
}
我已经从第一个接收点附加了代码
I've attached the code from the first receiving point
_networkReader = BufferedInputStream(BufferedInputStream(_socket!!.getInputStream()))
var bytesRead = _networkReader!!.read(buffer, 0, Companion.BUFFER_LEN)
if (bytesRead >= 0) {
if (!_connect) NotifyConnect()
var command = buffer[4]
var content: ByteArray? = null
var length : Int = 0
var index = 0
while (index + 3 < bytesRead) {
var contentlength: Int = ((buffer[index] and 0xff.toByte()).toInt() shl 24) + ((buffer[index + 1] and 0xff.toByte()).toInt() shl 16) + ((buffer[index + 2] and 0xff.toByte()).toInt() shl 8) + ((buffer[index + 3] and 0xff.toByte()).toInt() shl 0)
var cmd = buffer[index + 4]
if (bytesRead > Companion.COMMAND_LEN) {
content = ByteArray(bytesRead - index - Companion.COMMAND_LEN - 4)
length = content.size
System.arraycopy(buffer, index + 5, content, 0, contentlength)
}
processPacket(cmd, content, contentlength)
}
}
processPacket函数
processPacket Function
private fun processPacket(command: Byte, content: ByteArray?, length: Int) {
try {
if (content != null) {
LogUtil.i("StandElin 1-1 " + "Command 0x" + Integer.toHexString(command.toInt()) + " "
+ String(content, 0, length, CHARSET))
} else {
LogUtil.i("StandElin 1-2" + "Command 0x" + Integer.toHexString(command.toInt()))
}
LogUtil.i("StandElin 2 FinalCommand : 0x" + Integer.toHexString(command.toInt()))
var message: Message? = null
var parsed: Any? = null
DlgAprList.instance?.parseAndReplace(content)
最上面的第一个代码是DlgAprList类中包含的一个函数
The first code attached to the top is a function included in the DlgAprList class
推荐答案
I created a repo that you can run your use-cases against with tests: GitHub
有几个可能的问题:
-
Log.i
会引起任何麻烦吗? - 检查您使用的初始编码:
- Can
Log.i
cause any troubles? - Check the initial encoding you encode with:
val str = "My long string"
val byteArray = str.toByteArray(Charset.forName("utf-8"))
- 检查解码所使用的编码.在您的代码中,我看到
euc-kr
编码是硬编码的.这意味着,如果您使用utf-8编码并使用其他编码进行解码,则代码将失败. - Check the encoding you decode with. In your code, I see that
euc-kr
encoding is hard-coded. It means that if you encoded with utf-8 and decode with a different one, the code will fail.
这篇关于kotlin将bytearray转换为String数据崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!