Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8269205

Java cannot handle Byte Array conveniently

    XMLWordPrintable

Details

    • dio
    • x86_64
    • windows

    Description

      A DESCRIPTION OF THE PROBLEM :
              byte[] hexData = new byte[] { 0x01, 0x03, 0x04, 0x02, (byte)0x92, (byte) 0xFF, (byte)0x9B, 0x5A, 0x3D };

      but when you use it, you should use
              hexData[5]&0xFF

      but in many ways, like use java.io.DataInputStream.readShort(); to read the hexData[5] hexData[6] (0x92FF)
      you will read a wrong value, because in JVM, hexData[5] = (byte)0x92‘s value is
      0xffffffffffffff92

      in Modbus-RTU, the byte array handle is usual.
      And I has no other way to handle it, only to override java.io.DataOutputStream's read() using that code
          
          @Override
          public int read() throws IOException {
              return super.read()&0xFF;
          }


      Java really do no want to handle byte array, because it is no so convenient as C/Cpp or other language.


      Attachments

        Activity

          People

            Unassigned Unassigned
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated: