简体   繁体   English

java字节操作的疑问

[英]java byte operation doubt

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?? 对于此功能,某些情况,例如result[2] ,其中存储了0xD2 ,字节值作为[100,23] ...这些值没有按预期方式输出,然后...仅采用前半部分...怎么办我处理这种情况? for result[0] where it is just [123] , its fine... 对于result[0]仅在[123] ,它很好...

Do you know what String.getBytes() even does? 你知道String.getBytes()做什么吗? 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. 它将平台默认编码中的String对象转换为byte[] 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). 在位置0处获取byte只能告诉您平台默认编码中第一个字符是如何表示的(如果是单字节编码,则告诉您更少的信息)。

What do you want to achieve? 您想实现什么? You also use Integer.toHexString() . 您还可以使用Integer.toHexString() Could you give us an example what exactly you want the result to be when doing Integer.toHexString(100).getBytes()[0] ? 您能否举一个例子,说明执行Integer.toHexString(100).getBytes()[0]时要得到的结果到底是什么?

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

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> 我将commandMap等更改为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]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM