java字节操作疑问

发布于 2024-10-19 20:42:13 字数 1026 浏览 3 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

缱绻入梦 2024-10-26 20:42:13

您知道 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 a byte[] in the platform default encoding. Taking the byte at position 0 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 doing Integer.toHexString(100).getBytes()[0]?

记忆消瘦 2024-10-26 20:42:13

要将字符串“0xD0”转换为字节,我建议您使用 Integer.decode(String).byteValue() ,它可以处理 Java 样式 00x 数字/

I建议您尝试类似

byte[] result = { 
     '{',
     '{', 
     commandMap.get(commandName), 
     dataIdMap.get(dataId), 
     locationMap.get(memLocation), 
     (byte) (commandMap.get(commandName) + dataIdMap.get(dataId) + locationMap.get(memLocation)), 
     '}', 
     '}', 
     '\r' };
return result;

我将 commandMap 等更改为 Map类型的操作

编辑:这是一个更长的示例

String commandName = "";
String dataId = "";
String memLocation= "";
Map<String, Byte> commandMap = new LinkedHashMap<String, Byte>();
commandMap.put(commandName, Integer.decode("0xD2").byteValue());
Map<String, Byte> dataIdMap = new LinkedHashMap<String, Byte>();
dataIdMap.put(dataId, Integer.decode("0x1").byteValue());
Map<String, Byte> locationMap = new LinkedHashMap<String, Byte>();
locationMap.put(memLocation, Integer.decode("0x00").byteValue());

byte[] result = { '{', '{', commandMap.get(commandName), dataIdMap.get(dataId), locationMap.get(memLocation), (byte) (commandMap.get(commandName) + dataIdMap.get(dataId) + locationMap.get(memLocation)), '}', '}', '\r' };

System.out.println(Arrays.toString(result));

打印

[123, 123, -46, 1, 0, -45, 125, 125, 13]

To turn a String "0xD0" in to a byte I suggest you use Integer.decode(String).byteValue() which can handle Java style 0 and 0x numbers/

I suggest you try something like

byte[] result = { 
     '{',
     '{', 
     commandMap.get(commandName), 
     dataIdMap.get(dataId), 
     locationMap.get(memLocation), 
     (byte) (commandMap.get(commandName) + dataIdMap.get(dataId) + locationMap.get(memLocation)), 
     '}', 
     '}', 
     '\r' };
return result;

I would change commandMap etc. to be of type Map<String, Byte>

EDIT: Here is a longer example

String commandName = "";
String dataId = "";
String memLocation= "";
Map<String, Byte> commandMap = new LinkedHashMap<String, Byte>();
commandMap.put(commandName, Integer.decode("0xD2").byteValue());
Map<String, Byte> dataIdMap = new LinkedHashMap<String, Byte>();
dataIdMap.put(dataId, Integer.decode("0x1").byteValue());
Map<String, Byte> locationMap = new LinkedHashMap<String, Byte>();
locationMap.put(memLocation, Integer.decode("0x00").byteValue());

byte[] result = { '{', '{', commandMap.get(commandName), dataIdMap.get(dataId), locationMap.get(memLocation), (byte) (commandMap.get(commandName) + dataIdMap.get(dataId) + locationMap.get(memLocation)), '}', '}', '\r' };

System.out.println(Arrays.toString(result));

prints

[123, 123, -46, 1, 0, -45, 125, 125, 13]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文