我将如何选择第一个“ n”字节数组的元素并将其直接转换为字符串? [英] How would I take the first "n" elements of a byte array and convert them directly into a string?
本文介绍了我将如何选择第一个“ n”字节数组的元素并将其直接转换为字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个由1024个元素组成的字节数组。我想将其分解为不同的字符串私有成员(例如,名称的前9个字节,用户ID的后12个字节,等等)。
I have an array of bytes consisting of 1024 elements. I want to break this down into different string private members (e.g. first 9 bytes for name, next 12 bytes for userID, etc.).
不必翻整个字节数组转换为字符串,然后使用子字符串方法,有什么办法可以将数组中的字节范围直接转换为班级的私有成员?
Without having to turn the entire byte array into a string and then using a substring method, is there any way I can turn a range of bytes in the array directly into a private member for my class?
例如,
myObject.name = byteArr[0-9];
myObject.userId = byteArr[10-21];
推荐答案
使用:
String myField = new String(myArray, start, end);
其中,开始
为0从头开始
这篇关于我将如何选择第一个“ n”字节数组的元素并将其直接转换为字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文