4

I am trying to write hexadecimal data into my serial port using java, but now I cant convert the hexadecimal data into the byte array.

Here is the code which shows the error message:

static byte[] bytearray = {0x02, 0x08, 0x16, 0x0, 0x00, 0x33, 0xC6, 0x1B};

This is the code writing into the serial port:

try {
        outputStream = serialPort.getOutputStream();
        // Write the stream of data conforming to PC to reader protocol
        outputStream.write(bytearray);
        outputStream.flush();

        System.out.println("The following bytes are being written");
        for(int i=0; i<bytearray.length; i++){
            System.out.println(bytearray[i]);
            System.out.println("Tag will be read when its in the field of the reader");
        }
} catch (IOException e) {}

Can I know how can I solve this problem. Currently I am using the javax.comm plugin. Thank you.

raaj5671
  • 105
  • 4
  • 12
  • possible duplicate of [lossy conversion from int to byte?](http://stackoverflow.com/questions/28623477/lossy-conversion-from-int-to-byte) – Marvin Jun 22 '15 at 16:20

2 Answers2

3

If you look at the error message:

Main.java:10: error: incompatible types: possible lossy conversion from int to byte
    static byte[] bytearray = {0x02, 0x08, 0x16, 0x0, 0x00, 0x33, 0xC6, 0x1B};
                                                                  ^

There is a small caret pointing to the value 0xC6. The reason for the issue is that java's byte is signed, meaning that its range is from -0x80 to 0x7F. You can fix this by casting:

    static byte[] bytearray = {0x02, 0x08, 0x16, 0x0, 0x00, 0x33, (byte) 0xC6, 0x1B};

Or, you can use the negative, in-range value of -0x3A (which is equivalent to 0x36 in two's-complement notation).

nanofarad
  • 40,330
  • 4
  • 86
  • 117
  • well thanks a lot that solved my problem. But just for future reference can I know what you meant by the small caret pointing. Are there any ways to find out that? Thank you. – raaj5671 Jun 22 '15 at 16:25
  • @user3833653 In the error message, when compiled on `javac`, a little `^` will point to the part of the syntax that is an error. You can see it in the block of code that contained your error. In an IDE, you should get a red underline similar to a spelling error in a word processor. – nanofarad Jun 22 '15 at 16:27
  • well actually the oxC6 should be 198 in decimal but mine after i put the byte its showing 58 or -58. Can I know how to solve this? @hexafraction – raaj5671 Jun 23 '15 at 00:57
  • -58 in 2s complement is the same pattern of bits as 198 unsigned. – nanofarad Jun 23 '15 at 09:41
0

Try to cast 0xC6 like this as byte range is from -0x80 to 0x7F:

static byte[] bytearray = {0x02, 0x08, 0x16, 0x0, 0x00, 0x33, (byte) 0xC6, 0x1B};
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
  • 1
    well the helped to solve my error but it didnt get the answer which i am looking for. Actually C6 should be 198 in decimal but then after adding the byte infront as you said i am getting -58 decimal value. can i know how to solve this? – raaj5671 Jun 23 '15 at 01:23