java字节操作疑问
public final byte[] getParam(String commandName,String memLocation,String dataId){
byte[] result = new byte[9];
//"GET_PARAM", "RAM","WATER_OUTLET_TEMP"
result[0] = START_FRAME.getBytes()[0]; // {
result[1] = START_FRAME.getBytes()[0]; // {
result[2] = Integer.toHexString(commandMap.get(commandName)).getBytes()[0]; // 0xD2
result[3] = Integer.toHexString(dataIdMap.get(dataId)).getBytes()[0]; // 0x1
result[4] = Integer.toHexString(locationMap.get(memLocation)).getBytes()[0]; //0x00
result[5] = Integer.toHexString(commandMap.get(commandName) + dataIdMap.get(dataId) + locationMap.get(memLocation)).getBytes()[0];
result[6] = END_FRAME.getBytes()[0]; // }
result[7] = END_FRAME.getBytes()[0]; // }
result[8] = END_OF_LINE.getBytes()[0]; // \r
return result;
}
对于此函数的某些情况,例如 result[2]
,其中存储 0xD2
并且字节值为 [100,23]
...然后值没有按预期输出...只取了前半部分...我该如何处理这种情况?对于result[0]
,它只是[123]
,它很好......
public final byte[] getParam(String commandName,String memLocation,String dataId){
byte[] result = new byte[9];
//"GET_PARAM", "RAM","WATER_OUTLET_TEMP"
result[0] = START_FRAME.getBytes()[0]; // {
result[1] = START_FRAME.getBytes()[0]; // {
result[2] = Integer.toHexString(commandMap.get(commandName)).getBytes()[0]; // 0xD2
result[3] = Integer.toHexString(dataIdMap.get(dataId)).getBytes()[0]; // 0x1
result[4] = Integer.toHexString(locationMap.get(memLocation)).getBytes()[0]; //0x00
result[5] = Integer.toHexString(commandMap.get(commandName) + dataIdMap.get(dataId) + locationMap.get(memLocation)).getBytes()[0];
result[6] = END_FRAME.getBytes()[0]; // }
result[7] = END_FRAME.getBytes()[0]; // }
result[8] = END_OF_LINE.getBytes()[0]; // \r
return result;
}
For this function certain cases like result[2]
where 0xD2
is stored and the bytes value come as [100,23]
...the values dont come out as expected then...only the first half is taken... how do i handle such cases?? for result[0]
where it is just [123]
, its fine...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您知道
String.getBytes()
的用途吗?老实说,这段代码看起来你不知道,或者你期望它做一些它不做的事情。它将
String
对象转换为平台默认编码的byte[]
。取位置0
处的byte
只能告诉你第一个字符在平台默认编码中是如何表示的(如果它是单字节编码,否则它告诉你的就更少了) )。你想实现什么目标?您还可以使用
Integer.toHexString()
。您能否给我们举个例子,您在执行Integer.toHexString(100).getBytes()[0]
时希望得到的结果是什么?Do you know what
String.getBytes()
even does? To be honest, this code looks like you don't know or you expect it to do something that it doesn't.It converts the
String
object to abyte[]
in the platform default encoding. Taking thebyte
at position0
only tells you how the first character is represented in the platform default encoding (if it is a singly-byte encoding, otherwise it tells you even less).What do you want to achieve? You also use
Integer.toHexString()
. Could you give us an example what exactly you want the result to be when doingInteger.toHexString(100).getBytes()[0]
?要将字符串“0xD0”转换为字节,我建议您使用 Integer.decode(String).byteValue() ,它可以处理 Java 样式
0
和0x
数字/I建议您尝试类似
我将 commandMap 等更改为 Map类型的操作
编辑:这是一个更长的示例
打印
To turn a String "0xD0" in to a byte I suggest you use Integer.decode(String).byteValue() which can handle Java style
0
and0x
numbers/I suggest you try something like
I would change commandMap etc. to be of type Map<String, Byte>
EDIT: Here is a longer example
prints