开发者

java byte operation doubt

public final byte[] getParam(String commandName,String memLocation,String dataId){
    byte[] re开发者_如何学JAVAsult = 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...


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]?


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]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜